Java Reference
In-Depth Information
If you add another test to the
CalculatorTest
class, like
testSubtract
, and you
annotate it with the
@Test
, the default
Suite
would automatically include it.
The
Suite
object is a
Runner
that executes all of the
@Test
annotated methods
in the test class. Listing 2.3 shows how to compose multiple test classes in a single
test suite.
Listing 2.3
Composing a
Suite
from test classes
[...]
@RunWith(value=org.junit.runners.Suite.
class
)
@SuiteClasses(value={FolderConfigurationTest.
class
,
FileConfigurationTest.
class
})
public
class
FileSystemConfigurationTestSuite {
}
In listing 2.3, we specify the appropriate runner with the
@RunWith
annotation
B
and
list the tests we want to include in this test by specifying the test classes in the
@Suite-
Classes
annotation
C
. All the
@Test
methods from these classes will be included in
the
Suite
.
For the
CalculatorTest
in listing 2.1, you can represent the default
Suite
like this:
B
C
@RunWith(value=Suite.class)
@SuiteClasses(value={CalculatorTest.class})
public class AllTests {
}
2.4.2
Composing a suite of suites
Because of the clever way
JU
nit is constructed, it's possible to create a suite of test
suites. For example, listing 2.4 concatenates various files to show how test cases make
up suites, which in turn make up a master suite.
Listing 2.4
Suite of suites
[...]
public class
TestCaseA {
@Test
public void
testA1() {
// omitted
}
}
[...]
public class
TestCaseB {
@Test
public void
testB1() {
// omitted
}
}
[...]
@RunWith(value=Suite
.class
)
@SuiteClasses(value = {TestCaseA
.class
})