Merge pull request #9970 from DSpace/backport-9968-to-dspace-8_x

[Port dspace-8_x] Improve logging of Unit & Integration Tests.  Ensure all tests log which method they are running.
This commit is contained in:
Tim Donohue
2024-11-06 15:53:15 -06:00
committed by GitHub
5 changed files with 47 additions and 52 deletions

View File

@@ -21,8 +21,12 @@ import org.dspace.builder.AbstractBuilder;
import org.dspace.discovery.SearchUtils;
import org.dspace.servicemanager.DSpaceKernelImpl;
import org.dspace.servicemanager.DSpaceKernelInit;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.TestName;
/**
* Abstract Test class copied from DSpace API
@@ -46,6 +50,12 @@ public class AbstractDSpaceIntegrationTest {
*/
protected static DSpaceKernelImpl kernelImpl;
/**
* Obtain the TestName from JUnit, so that we can print it out in the test logs (see below)
*/
@Rule
public TestName testName = new TestName();
/**
* Default constructor
*/
@@ -90,6 +100,20 @@ public class AbstractDSpaceIntegrationTest {
}
}
@Before
public void printTestMethodBefore() {
// Log the test method being executed. Put lines around it to make it stand out.
log.info("---");
log.info("Starting execution of test method: {}()", testName.getMethodName());
log.info("---");
}
@After
public void printTestMethodAfter() {
// Log the test method just completed.
log.info("Finished execution of test method: {}()", testName.getMethodName());
}
/**
* This method will be run after all tests finish as per @AfterClass. It
* will clean resources initialized by the @BeforeClass methods.

View File

@@ -18,9 +18,13 @@ import java.util.TimeZone;
import org.apache.logging.log4j.Logger;
import org.dspace.servicemanager.DSpaceKernelImpl;
import org.dspace.servicemanager.DSpaceKernelInit;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
@@ -62,6 +66,12 @@ public class AbstractDSpaceTest {
*/
protected static DSpaceKernelImpl kernelImpl;
/**
* Obtain the TestName from JUnit, so that we can print it out in the test logs (see below)
*/
@Rule
public TestName testName = new TestName();
/**
* This method will be run before the first test as per @BeforeClass. It will
* initialize shared resources required for all tests of this class.
@@ -94,6 +104,19 @@ public class AbstractDSpaceTest {
}
}
@Before
public void printTestMethodBefore() {
// Log the test method being executed. Put lines around it to make it stand out.
log.info("---");
log.info("Starting execution of test method: {}()", testName.getMethodName());
log.info("---");
}
@After
public void printTestMethodAfter() {
// Log the test method just completed.
log.info("Finished execution of test method: {}()", testName.getMethodName());
}
/**
* This method will be run after all tests finish as per @AfterClass. It

View File

@@ -42,7 +42,6 @@ import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@@ -80,9 +79,6 @@ import org.springframework.web.context.WebApplicationContext;
@WebAppConfiguration
// Load our src/test/resources/application-test.properties to override some settings in default application.properties
@TestPropertySource(locations = "classpath:application-test.properties")
// Enable our custom Logging listener to log when each test method starts/stops
@TestExecutionListeners(listeners = {LoggingTestExecutionListener.class},
mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
public class AbstractControllerIntegrationTest extends AbstractIntegrationTestWithDatabase {
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger();

View File

@@ -22,7 +22,6 @@ import org.springframework.http.HttpEntity;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
@@ -52,9 +51,6 @@ import org.springframework.test.context.junit4.SpringRunner;
@ContextConfiguration(initializers = { DSpaceKernelInitializer.class, DSpaceConfigurationInitializer.class })
// Load our src/test/resources/application-test.properties to override some settings in default application.properties
@TestPropertySource(locations = "classpath:application-test.properties")
// Enable our custom Logging listener to log when each test method starts/stops
@TestExecutionListeners(listeners = {LoggingTestExecutionListener.class},
mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
public class AbstractWebClientIntegrationTest extends AbstractIntegrationTestWithDatabase {
// (Random) port chosen for test web server
@LocalServerPort

View File

@@ -1,44 +0,0 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest.test;
import org.apache.logging.log4j.Logger;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.support.AbstractTestExecutionListener;
/**
* Custom DSpace TestExecutionListener which logs messages whenever a specific Test Case (i.e. test method) has
* started or ended execution. This makes Test environment logs easier to read/understand as you know which method has
* caused errors, etc.
*/
public class LoggingTestExecutionListener extends AbstractTestExecutionListener {
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(LoggingTestExecutionListener.class);
/**
* Before each test method is run
* @param testContext
*/
@Override
public void beforeTestMethod(TestContext testContext) {
// Log the test method being executed. Put lines around it to make it stand out.
log.info("---");
log.info("Starting execution of test method: {}()", testContext.getTestMethod().getName());
log.info("---");
}
/**
* After each test method is run
* @param testContext
*/
@Override
public void afterTestMethod(TestContext testContext) {
// Log the test method just completed.
log.info("Finished execution of test method: {}()", testContext.getTestMethod().getName());
}
}