Skip to content

Commit

Permalink
Merge pull request DSpace#9968 from tdonohue/improve_test_logging
Browse files Browse the repository at this point in the history
Improve logging of Unit & Integration Tests.  Ensure all tests log which method they are running.
  • Loading branch information
tdonohue authored Nov 6, 2024
2 parents b034b07 + bd20c92 commit 76355af
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
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
Expand All @@ -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
*/
Expand Down Expand Up @@ -90,6 +100,20 @@ public static void initTestEnvironment() {
}
}

@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.
Expand Down
23 changes: 23 additions & 0 deletions dspace-api/src/test/java/org/dspace/AbstractDSpaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
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;

Expand Down Expand Up @@ -62,6 +66,12 @@ protected 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.
Expand Down Expand Up @@ -94,6 +104,19 @@ public static void initKernel() {
}
}

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
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;
Expand Down Expand Up @@ -80,9 +79,6 @@
@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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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;

Expand Down Expand Up @@ -52,9 +51,6 @@
@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
Expand Down

This file was deleted.

0 comments on commit 76355af

Please sign in to comment.