mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 01:54:22 +00:00
Remove @DirtiesContext and allow reuse of Spring ApplicationContext & DSpace Kernel
This commit is contained in:
@@ -33,7 +33,6 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
@@ -58,8 +57,6 @@ import org.springframework.web.context.WebApplicationContext;
|
||||
@SpringBootTest(classes = Application.class)
|
||||
// Load DSpaceKernelInitializer in Spring ApplicationContext (to initialize DSpace Kernel)
|
||||
@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)
|
||||
@WebAppConfiguration
|
||||
public class AbstractControllerIntegrationTest extends AbstractIntegrationTestWithDatabase {
|
||||
|
@@ -54,11 +54,10 @@ public class AbstractDSpaceIntegrationTest {
|
||||
* This method will be run before the first test as per @BeforeClass. It will
|
||||
* initialize shared resources required for all tests of this class.
|
||||
*
|
||||
* This method loads our test properties to initialize our test environment,
|
||||
* and then starts the DSpace Kernel (which allows access to services).
|
||||
* This method loads our test properties for usage in test environment.
|
||||
*/
|
||||
@BeforeClass
|
||||
public static void initKernel() {
|
||||
public static void initTestEnvironment() {
|
||||
try {
|
||||
//Stops System.exit(0) throws exception instead of exitting
|
||||
System.setSecurityManager(new NoExitSecurityManager());
|
||||
@@ -72,12 +71,17 @@ public class AbstractDSpaceIntegrationTest {
|
||||
.getResource("test-config.properties");
|
||||
testProps.load(properties.openStream());
|
||||
|
||||
// Initialise the service manager kernel
|
||||
// Get a reference to current Kernel
|
||||
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()) {
|
||||
// NOTE: the "dspace.dir" system property MUST be specified via Maven
|
||||
kernelImpl.start(getDspaceDir()); // init the kernel
|
||||
}
|
||||
|
||||
// Initialize our builder (by loading all DSpace services)
|
||||
AbstractBuilder.init();
|
||||
} catch (IOException ex) {
|
||||
log.error("Error initializing tests", ex);
|
||||
@@ -90,20 +94,20 @@ public class AbstractDSpaceIntegrationTest {
|
||||
* will clean resources initialized by the @BeforeClass methods.
|
||||
*/
|
||||
@AfterClass
|
||||
public static void destroyKernel() throws SQLException {
|
||||
public static void destroyTestEnvironment() throws SQLException {
|
||||
System.setSecurityManager(null);
|
||||
|
||||
//we clear the properties
|
||||
// Clear our test properties
|
||||
testProps.clear();
|
||||
testProps = null;
|
||||
|
||||
// Unload DSpace services
|
||||
AbstractBuilder.destroy();
|
||||
|
||||
//Also clear out the kernel & nullify (so JUnit will clean it up)
|
||||
if (kernelImpl != null) {
|
||||
kernelImpl.destroy();
|
||||
}
|
||||
kernelImpl = null;
|
||||
// NOTE: We explicitly do NOT stop/destroy the kernel, as it is cached
|
||||
// in the Spring ApplicationContext. By default, to speed up tests,
|
||||
// Spring caches & reuses its ApplicationContext for all tests. So,
|
||||
// similarly, our kernel is being cached & reused for all tests.
|
||||
}
|
||||
|
||||
public static String getDspaceDir() {
|
||||
|
Reference in New Issue
Block a user