Java Reference
In-Depth Information
Table 2.2
JUnit core objects
JUnit concept
Responsibilities
Introduced in
Lets you define the conditions that you want to test. An assert
method is silent when its proposition succeeds but throws an
exception if the proposition fails.
Section 2.1
Assert
Test
A method with a @Test annotation defines a test. To run this
method JUnit constructs a new instance of the containing class
and then invokes the annotated method.
Section 2.1
Test class
Section 2.1
A test class is the container for @Test methods.
Suite
The Suite allows you to group test classes together.
Section 2.3
Section 2.2
Runner
The Runner class runs tests. JUnit 4 is backward compatible and
will run JUnit 3 tests.
Let's take a closer look at the responsibilities of each of the core objects that make up
JU nit; see table 2.2.
We can move on to explaining in detail the objects from this table that we've not
seen yet: the test Runner and test Suite objects.
To run a basic test class, you needn't do anything special; JU nit uses a test runner on
your behalf to manage the lifecycle of your test class, including creating the class,
invoking tests, and gathering results. The next sections address situations that may
require you to set up your test to run in a special manner. One of these situations alle-
viates a common problem when creating tests: invoking tests with different inputs. We
discuss this specific scenario with an example in the next section before looking at the
remaining test runners provided by JU nit.
2.2
Running parameterized tests
The Parameterized test runner allows you to run a test many times with different sets
of parameters. Listing 2.2 demonstrates the Parameterized runner in action (you can
find this test in the source code samples for chapter 1).
Listing 2.2
Parameterized tests
[...]
@RunWith(value=Parameterized. class )
public class ParameterizedTest {
B
C
private double expected;
private double valueOne;
private double valueTwo;
D
@Parameters
public static Collection<Integer[]> getTestParameters() {
return Arrays.asList( new Integer[][] {
 
 
 
 
 
 
 
 
 
 
Search WWH ::




Custom Search