Remove @DirtiesContext and allow reuse of Spring ApplicationContext & DSpace Kernel

This commit is contained in:
Tim Donohue
2018-12-11 21:55:57 +00:00
parent c2803a7f69
commit c305879b51
2 changed files with 15 additions and 14 deletions

View File

@@ -33,7 +33,6 @@ import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
@@ -58,8 +57,6 @@ import org.springframework.web.context.WebApplicationContext;
@SpringBootTest(classes = Application.class) @SpringBootTest(classes = Application.class)
// Load DSpaceKernelInitializer in Spring ApplicationContext (to initialize DSpace Kernel) // Load DSpaceKernelInitializer in Spring ApplicationContext (to initialize DSpace Kernel)
@ContextConfiguration(initializers = DSpaceKernelInitializer.class) @ContextConfiguration(initializers = DSpaceKernelInitializer.class)
// Tell Spring to remove all cached Contexts and rebuild ApplicationContext *for each test class*
@DirtiesContext
// Tell Spring to make ApplicationContext an instance of WebApplicationContext (for web-based tests) // Tell Spring to make ApplicationContext an instance of WebApplicationContext (for web-based tests)
@WebAppConfiguration @WebAppConfiguration
public class AbstractControllerIntegrationTest extends AbstractIntegrationTestWithDatabase { public class AbstractControllerIntegrationTest extends AbstractIntegrationTestWithDatabase {

View File

@@ -54,11 +54,10 @@ public class AbstractDSpaceIntegrationTest {
* This method will be run before the first test as per @BeforeClass. It will * This method will be run before the first test as per @BeforeClass. It will
* initialize shared resources required for all tests of this class. * initialize shared resources required for all tests of this class.
* *
* This method loads our test properties to initialize our test environment, * This method loads our test properties for usage in test environment.
* and then starts the DSpace Kernel (which allows access to services).
*/ */
@BeforeClass @BeforeClass
public static void initKernel() { public static void initTestEnvironment() {
try { try {
//Stops System.exit(0) throws exception instead of exitting //Stops System.exit(0) throws exception instead of exitting
System.setSecurityManager(new NoExitSecurityManager()); System.setSecurityManager(new NoExitSecurityManager());
@@ -72,12 +71,17 @@ public class AbstractDSpaceIntegrationTest {
.getResource("test-config.properties"); .getResource("test-config.properties");
testProps.load(properties.openStream()); testProps.load(properties.openStream());
// Initialise the service manager kernel // Get a reference to current Kernel
kernelImpl = DSpaceKernelInit.getKernel(null); kernelImpl = DSpaceKernelInit.getKernel(null);
// If somehow the kernel is NOT initialized, initialize it.
// NOTE: This is likely never going to occur, as Spring Boot initializes it
// See AbstractControllerIntegrationTest (where @SpringBootTest is defined)
if (!kernelImpl.isRunning()) { if (!kernelImpl.isRunning()) {
// NOTE: the "dspace.dir" system property MUST be specified via Maven // NOTE: the "dspace.dir" system property MUST be specified via Maven
kernelImpl.start(getDspaceDir()); // init the kernel kernelImpl.start(getDspaceDir()); // init the kernel
} }
// Initialize our builder (by loading all DSpace services)
AbstractBuilder.init(); AbstractBuilder.init();
} catch (IOException ex) { } catch (IOException ex) {
log.error("Error initializing tests", ex); log.error("Error initializing tests", ex);
@@ -90,20 +94,20 @@ public class AbstractDSpaceIntegrationTest {
* will clean resources initialized by the @BeforeClass methods. * will clean resources initialized by the @BeforeClass methods.
*/ */
@AfterClass @AfterClass
public static void destroyKernel() throws SQLException { public static void destroyTestEnvironment() throws SQLException {
System.setSecurityManager(null); System.setSecurityManager(null);
//we clear the properties // Clear our test properties
testProps.clear(); testProps.clear();
testProps = null; testProps = null;
// Unload DSpace services
AbstractBuilder.destroy(); AbstractBuilder.destroy();
//Also clear out the kernel & nullify (so JUnit will clean it up) // NOTE: We explicitly do NOT stop/destroy the kernel, as it is cached
if (kernelImpl != null) { // in the Spring ApplicationContext. By default, to speed up tests,
kernelImpl.destroy(); // Spring caches & reuses its ApplicationContext for all tests. So,
} // similarly, our kernel is being cached & reused for all tests.
kernelImpl = null;
} }
public static String getDspaceDir() { public static String getDspaceDir() {