If your test case needs disk access then, the Pineapple test utilities contains a Spring TestExecutionListener implementation, which creates a new directory prior to execution of a test method in a test class. Each test method is then free to create additional sub directories or use files in this directory.
The execution listener can be configured to delete the directory after execution of each test method.
To write unit tests that requires disk access follow these steps:
The Spring-test jar gives access to:
To use the Spring framework test facilities, add the Maven dependency to the project POM:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <scope>test</scope> </dependency>
Version information is located in the pineapple-project/pom.xml. The dependency is scoped as test as it should only be available in the test phase.
Create the JUnit test class which should contain the tests which needs access to a separate test directory for each test method.
Annotate the class with the @RunWith( SpringJUnit4ClassRunner.class ) Spring annotation to configure the class to be run with the Spring JUnit class runner.
Example: The integration class BasicHtmlReportGeneratorIntegrationTest in the pineapple-basic-html-report-generator project is configured with:
Annotate the class with the @ContextConfiguration( locations = "/some-app-config-config.xml" ) Spring annotation to configure the Spring JUnit class runner to load the application context from the designated location(s).
Example: The integration class BasicHtmlReportGeneratorIntegrationTest in the pineapple-basic-html-report-generator project is configured with:
Annotate the class with the @TestExecutionListeners( DirectoryTestExecutionListener.class ) Spring annotation to configure the Spring test runner to use the DirectoryTestExecutionListener class during test execution.
The DirectoryTestExecutionListener class creates a test directory named ${class-name}-${method-name} prior to execution of the test method named ${method-name} in the test class ${class-name}. After execution of the test method the directory is deleted if the DirectoryTestExecutionListener is configured to do so.
Spring 3.0 contains three default TestExecutionListener implementation's which needs to be configured additionally with the @TestExecutionListeners if their functionality should be enabled:
Link to the Spring 3.0 documentation about the default TestExecutionListener implementations.
Please notice: if the test case is a integration test then the DependencyInjectionTestExecutionListener should used to enable the dependency injection.
The name of the test directory for the current test method can be looked up from a test method using the static method DirectoryTestExecutionListener.getCurrentTestDirectory().
The method can be invoked in the setUp() for test class, as shown in the example below.
Dependency injection will be disabled in this example.
@RunWith( SpringJUnit4ClassRunner.class ) @TestExecutionListeners( DirectoryTestExecutionListener.class ) @ContextConfiguration( locations = { "/com.alpha.pineapple.report.basichtml-config.xml" } ) public class BasicHtmlReportGeneratorIntegrationTest { /** * Current test directory. */ File testDirectory; @Before public void setUp() throws Exception { // get the test directory testDirectory = DirectoryTestExecutionListener.getCurrentTestDirectory(); } @After public void tearDown() throws Exception { testDirectory = null; } @Test public void testCanCreateSimpleReport() { // getting the test directory for this test method File rootDirectory = new File(testDirectory, "reports" ); // remaining test logic.... } }
Dependency injection will be enabled in this example.
@RunWith( SpringJUnit4ClassRunner.class ) @TestExecutionListeners( {DirectoryTestExecutionListener.class, DependencyInjectionTestExecutionListener.class}) @ContextConfiguration( locations = { "/com.alpha.pineapple.report.basichtml-config.xml" } ) public class BasicHtmlReportGeneratorIntegrationTest { /** * Current test directory. */ File testDirectory; @Before public void setUp() throws Exception { // get the test directory testDirectory = DirectoryTestExecutionListener.getCurrentTestDirectory(); } @After public void tearDown() throws Exception { testDirectory = null; } @Test public void testCanCreateSimpleReport() { // getting the test directory for this test method File rootDirectory = new File(testDirectory, "reports" ); // remaining test logic.... } }