Java Reference
In-Depth Information
B
public class TestDefaultController
{
private DefaultController controller;
@Before
public void instantiate() throws Exception
{
controller = new DefaultController();
}
@Test
public void testMethod()
{
throw new RuntimeException("implement me");
}
}
Start the name of the test case class with the prefix Test B . The naming convention
isn't required, but by doing so, we mark the class as a test case so that we can easily rec-
ognize test classes and possibly filter them in build scripts. Alternatively, and depend-
ing on your native language, you may prefer to postfix class names with Test .
Next, use the @Before annotated method to instantiate DefaultController C .
This is a built-in extension point that the JU nit framework calls between test methods.
At D you insert a dummy test method, so you have something to run. As soon as
you're sure the test infrastructure is working, you can begin adding real test methods.
Although this test runs, it also fails. The next step is to fix the test!
Use a best practice by throwing an exception for test code that you haven't imple-
mented yet E . This prevents the test from passing and reminds you that you must
implement this code.
Now that you have a bootstrap test, the next step is to decide what to test first.
C
D
E
JUnit's details
The @Before and @After annotated methods are executed right before/after the
execution of each one of your @Test methods and regardless of whether the test
failed or not. This helps you to extract all of your common logic, like instantiating
your domain objects and setting them up in some known state. You can have as
many of these methods as you want, but beware that if you have more than one of
the @Before/@After methods, the order of their execution is not defined.
JUnit also provides the @BeforeClass and @AfterClass annotations to anno-
tate your methods in that class. The methods that you annotate will get exe-
cuted, only once, before/after all of your @Test methods. Again, as with the
@Before and @After annotations, you can have as many of these methods as
you want, and again the order of the execution is unspecified.
You need to remember that both the @Before/@After and @BeforeClass/@After-
Class annotated methods must be public . The @BeforeClass/@AfterClass
annotated methods must be public and static .
 
 
 
 
 
 
Search WWH ::




Custom Search