开发者

Junit Test: what is Failure?

What exactly does it mean if the result of a junit test is "Failed" ? i assume that an excep开发者_Go百科tion happened during the test, but i can't find any information about it. also the line in the failure trace is:

Assert.assertTrue(result);

where result is just a boolean. so i dont know how an exception could happen in this line.


Assert.assertTrue(result);

This assert will fail if result is false, and you will get an AssertionError. The writer of the test wants to make sure that the result is true, and if it is false then something is wrong and you should check the code under test above.


Your question seems to be the difference between a "Failiure" and an "Error", since both ends up corresponding to an Exception being thrown.

A "Failure" is a place in your test where something that you expect does not happen. In a test like :

  TestedObject testedObject = new TestedObject(); // This is the object we test
  boolean something = testedObject.computeSomething(); // The tested method
  Assert.assertTrue("Something should be true", something);
  // following of the test

If the result of your computation (the 'something' variable), is not true, it means that the computeSomething method is working, but not as expected ; this is a "Failure". The cause is probably a logical bug in the method.

If the computeSomething() method throws an unexpected exception (a NPE in the computation, or whatever), than this also means that the method is broken, but potentially in a more 'brutal' way (a dependency missing, a corner case that is not handled, etc..). There needs to be some exception handling in the method. This would be called an 'Error' in JUnit parlance.

Now, in both cases, it means that the method is broken, and an Exception will be thrown (either by your code, or by the Assert.assertXXX methods) and caught by the TestRunner. It is also not uncommon to simply say in both case that "the test is failing" ; because what matters is that there is some work to be done in the TestedObject to get the green bar back.

Of course, sometimes, you want to test that your code actually throws an Exception, so you would write something like :

  TestedObject testedObject = new TestedObject(); // This is the object we test
  try {
    // The tested method, that is expected to throw an Exeption when given null
    boolean something = testedObject.computeSomething(null); 
    Assert.fail("The computation should have failed");
  } catch (IllegalArgumentException e) {
    // Pass - This is the expected behavior
  }

In this case throwing an exception should cause neither an Error nor a Failure ; it is on the contrary when the Exception is not thrown that the test should fail.


Failed usually means that an assertion failed. Exceptions are tracked separately (under errors, iirc).


When a JUnit test is "Failed", u have gotten an AssertionException. Means in your case result was false, where it should have been true.


From the documentation -

A Failure holds a description of the failed test and the exception that was thrown while running it. In most cases the Description will be of a single test. However, if problems are encountered while constructing the test (for example, if a BeforeClass method is not static), it may describe something other than a single test.

Generally, a failure means that your assertion about the outcome of the test has failed.


This just means, the value of your boolean variable result is false.

Assert.assertTrue(result);

means, you want to check if "result" is true. Note the method name assertTrue. The testcase can pass if and only if result=true. Otherwise, the case is considered to be a fail.

When you dont get what you expect, it is a failure. It need not be an exception. If you expect the code routine to return X and what you get is Y then its a failed test case.


It does not mean an exception occurred, only that the code returned another value than your test asserted. Replace your code with
Assert.asserFalse(result);
And see if this test doesn't fail

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜