⬆️ ⬇️

How to write unit tests if you don’t feel like

All of us at work every now and then try to get to write unit tests. Many have already understood that from them one harm. Writing tests takes a lot of time for which you could do something more useful. If the test suddenly begins to fall, the assembly on the server of continuous integration breaks down, the release does not roll out in time, the business loses money and you, the author of the fallen unit test, are the last. When refactoring, tests cause a headache, because they begin to fall and you have to deal with it.



Nevertheless, evil supervisors require more tests, talking about the so-called "quality control". Particularly tricky managers even consider coverage and do not let you go until you reach it. Your code is wrapped in a review if there are no tests in it or if they didn’t like something. Sheer frustration!



What to do?



Fortunately, there are ways to write reliable unit tests that never fall. I did not come up with these methods, they are successfully practiced in a number of open-source projects. All the examples I give are taken from the real code. Therefore, there is no reason for you not to take advantage of what is already being practiced by other developers!



The first and most obvious way: do not check anything in the unit test. Here is a simple example :



public void testSetFile() {
    System.out.println("setFile");
    File f = null;
    BlastXMLParser instance = new BlastXMLParser();
    instance.setFile(f);
}


? , , . - , null null'. , .



? :



@Test
public void getParametersTest() {
    List<IGeneratorParameter<?>> parameters = generator.getParameters();
    containsParameterType(parameters, AtomColor.class);
    containsParameterType(parameters, AtomColorer.class);
    containsParameterType(parameters, AtomRadius.class);
    containsParameterType(parameters, ColorByType.class);
    ...
}


, - . containsParameterType:



public <T> boolean containsParameterType(List<IGeneratorParameter<?>> list, Class<T> type) {
    for (IGeneratorParameter<?> item : list) {
        if (item.getClass().getName().equals(type.getName())) return true;
    }
    return false;
}


, ? , . , . !



. , . - . , , , . :



for (int i = 0; i < 0; i++)
{
    Assert.assertTrue(errorProbabilities[i] > 0.0d);
}


0 . . :



List<JavaOperationSignature> sigs = new ArrayList<>();
List<JavaOperationSignature> sigs2 = new ArrayList<>();

for (int i = 0; i < sigs.size(); i++) { //  ,   
    sigs.add(JavaOperationSignature.buildFor(nodes.get(i)));
    sigs2.add(JavaOperationSignature.buildFor(nodes2.get(i)));
}

for (int i = 0; i < sigs.size() - 1; i++) { //  ,  
    assertTrue(sigs.get(i) == sigs.get(i + 1));
    assertTrue(sigs2.get(i) == sigs2.get(i + 1));
}


! , — . .



, , , . , catch. , . , :



try {
    getDs().save(e);
} catch (Exception ex) {
    return; //     !
}

//   ,  -   
Assert.assertFalse("Should have got rejection for dot in field names", true); 
//     
e = getDs().get(e);
Assert.assertEquals("a", e.mymap.get("a.b")); //     !
Assert.assertEquals("b", e.mymap.get("c.e.g"));


, ? ? , . , . , null:



Assert.assertNotNull(new Electronegativity());


new null, . . , , . — :



DocumentImplementation document = new DocumentImplementation(props);
assertNotNull(document.toString().contains(KEY));
assertNotNull(document.toString().contains(VALUE));


boolean Boolean, , , . , , true false. :



Assert.assertNotNull("could not get nr. of eqr: ", afpChain.getNrEQR());


. , , getNrEQR int .



, :



Assert.assertNotNull("Attempt to test atom type which is not defined in the " +
     getAtomTypeListName() + ": " + exception.getMessage());


? , - , - . , null, , .



, , , assertNotNull . ! , assertEquals , :



Assert.assertEquals(ac2.getAtomCount(), ac2.getAtomCount());


, , getAtomCount. - !



double, NaN. , assertNotEquals , . assertTrue:



Assert.assertTrue(result1.get(i) != Double.NaN);


, NaN , , .



assertTrue instanceof-:



Assert.assertNotNull(cf.getRealFormat());
Assert.assertNotNull(cf.getImaginaryFormat());
Assert.assertTrue(cf.getRealFormat() instanceof NumberFormat);
Assert.assertTrue(cf.getImaginaryFormat() instanceof NumberFormat);


getRealFormat getImaginaryFormat NumberFormat, instanceof . . .



, . , assertThat AssertJ (, assertThat(somethingIsTrue()) assertThat(somethingIsTrue()).is(true)). try { ... } catch(Throwable t) {} AssertionError. . , CompletableFuture.runAsync() . , , .



. . . , , . ---!



')

Source: https://habr.com/ru/post/434972/



All Articles