时不时地在工作中,他们试图迫使我们编写单元测试。 许多人已经意识到自己造成了一种伤害。 编写测试会花费很多时间,为此您可以做一些更有用的事情。 如果测试意外开始下降,在持续集成服务器上的组件崩溃,发布没有按时推出,企业就亏本了,下降的单元测试的作者您将陷入极端。 重构时,测试会引起头痛,因为它们开始下降,您必须应对它。
然而,邪恶的老板们需要更多的测试,谈论所谓的“质量控制”。 尤其是狡猾的经理甚至会考虑覆盖范围,并且在达成目标之前不会让您工作。 如果其中没有测试或他们不喜欢测试,则您的代码将包含在评论中。 彻底不高兴!
怎么办
幸运的是,有一些方法可以编写永不失败的可靠单元测试。 这些方法不是我发明的;它们已在许多开源项目中成功实践。 我将给出的所有示例均来自真实代码。 因此,没有理由,您也无法利用其他开发人员已经实施的功能!
最明显的第一种方法:不要在单元测试中检查任何内容。 这是一个简单的例子 :
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()
. , , .
. . . , , . ---!