Java Reference
In-Depth Information
A.3.3
Differences in ignoring a test
Sometimes, because of a system reconfiguration or a change of client requirements,
you need to skip the execution of a certain test method. In the 3.x version of JU nit,
the only way to do that is to comment the whole test method or rename the method.
This way, if you start commenting your test methods, you get no detailed statistics of
how many methods were skipped during test execution.
This is significantly improved in JU nit 4.x, by the introduction of the @Ignore
annotation. You can use it to annotate any of your test methods, with the result that
this method will be skipped at the time of test execution. The best part is that at the
end of the execution you get detailed statistics of not only how many of your tests were
successful or failed but also the number of the skipped tests.
One last thing—from version 4.3 on of JU nit, you can annotate not only test
methods but also the whole class with @Ignore . As a result, all of the tests in that
class will be skipped.
A.3.4
Static imports
As mentioned already, your test cases no longer need to extend the junit.frame-
work.TestCase class, whereas in the previous versions of JU nit the assert methods
would come from the junit.framework.TestCase class that you used to extend. The
question that you'll ask now is, “How on earth do I get the assert methods in my class?”
The answer to this question is one of the new features of Java 5: static imports.
With Java's static imports, you can write the code shown in listing A.5.
Listing A.5
Static imports in action
B
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CalculatorTest {
@Test
public void add() {
assertEquals(60, 50+10);
}
}
At B we use the static import feature to import one of the assert methods we need,
and later on, at C , we call it.
C
A.3.5
Exception testing
One of the most important aspects of unit testing is the ability to test exceptional situ-
ations in your code. You need to make sure that whenever an exception is thrown, it's
handled properly. In listing A.6 you can see how exception handling is done in the
previous version of JU nit.
 
 
 
 
 
 
 
Search WWH ::




Custom Search