Java Reference
In-Depth Information
is annotate the @Test method with an @Ignore annotation. Look at the code in list-
ing 3.16.
Listing 3.16
Ignoring a test method in JUnit 4.x
[...]
@Test(timeout=130)
@Ignore(value="Ignore for now until we decide a decent time-limit")
public void testProcessMultipleRequestTimeout()
{
[...]
}
As you can see, the only thing we've added is the @Ignore annotation B to the
method. This annotation accepts the value parameter, which lets us insert a message
as to why we skip the test.
B
JUnit best practice: always specify a reason for skipping a test
As you saw in the previous listing, we specified why we needed to skip the exe-
cution of the test. It's a good practice to do that. First, you notify your fellow
developers why you want to skip the execution of the test, and second, you
prove to yourself that you know what the test does, and you don't ignore it just
because it fails.
As we mentioned, in JU nit 3 the only way to skip the execution of a test method was to
rename it or comment it out. This gives you no information whatsoever as to how
many tests were skipped. In JU nit 4, when you annotate methods with @Ignore, you
get statistics that include how many tests JU nit skipped in addition to how many tests
passed and failed.
3.5
Introducing Hamcrest matchers
The statistics show that people are easily infected with the unit testing philosophy.
Once you get accustomed to writing tests and see how good it feels to have someone
protecting you from possible mistakes, you'll wonder how it was possible to live with-
out unit testing before.
As you write more and more unit tests and assertions, you'll inevitably encounter
the problem that some of the assertions are big and hard to read. For example, con-
sider the code in listing 3.17.
Listing 3.17
Cumbersome JUnit assert method
[...]
public class HamcrestTest {
private List<String> values;
 
 
 
 
 
 
 
 
Search WWH ::




Custom Search