Java Reference
In-Depth Information
Listing B.4
First interceptor— SampleLoggingIntercepto r
[...]
public class SampleLoggingInterceptor implements Interceptor {
B
public void interceptBefore() {
System.out.println( "Before-test" );
}
C
D
public void interceptAfter() {
System.out.println( "After-test" );
}
}
This implementation is simple, but it's enough for what we currently need. The code
implements the Interceptor interface B (to make it a valid interceptor according
to our terms) and gives body to the interceptBefore C and interceptAfter meth-
ods D .
We use our interceptor in conjunction with our InterceptorRunner . And we
use our JU nit runner just like we use any other JU nit runner. Let's examine it in
listing B.5.
Listing B.5
Executing a test case using our custom runner
[...]
B
@RunWith( InterceptorRunner. class )
@InterceptorClasses( value = { SampleLoggingInterceptor. class } )
public class TestCustomRunnerWithLoggingInterceptor {
@Before
public void setUp() {
System.out.println( "Real before" );
}
C
D
@Test
public void testDummy() {
assertTrue( true );
System.out.println( "Some text for test purpose" );
}
@After
public void tearDown() {
System.out.println( "Real after" );
}
}
The test case we provide starts with the @RunWith annotation. With this annotation we
indicate which JU nit runner we want to execute our tests; in this case we want the
InterceptorRunner that we just implemented B . We also provide an @Interceptor-
Classes annotation to hold the interceptors we want to plug into our execution C .
Then we start implementing our test case. In this scenario we have only one test
method, defined just for testing purposes D .
 
 
 
Search WWH ::




Custom Search