BAEL-3300 The Spring TestExecutionListener (#7933)

* Added module for article

* Update pom.xml

* Delete pom.xml

* Update and rename ForecastProcessorTest.java to ForecastProcessorUnitTest.java

* Updated code as per review comments

* Delete .gitignore

* Update pom.xml

* BAEL-2904 Moved files

* BAEL-2904 Deleted files from previous project

* BAEL-2904 Moved classes for method reference article

* Update pom.xml

* BAEL-2904 Updated README.md

* First draft

* BAEL-3300 Added more tests

* BAEL-3300 Removed unused imports

* BAEL-3300 Run dos2unix on README.md
This commit is contained in:
Priyesh Mashelkar 2019-10-07 04:42:45 +01:00 committed by Grzegorz Piwowarek
parent 7cbe9ac12c
commit 1d0596f23c
6 changed files with 123 additions and 0 deletions

View File

@ -0,0 +1,10 @@
package com.baeldung.testexecutionlisteners;
import org.springframework.stereotype.Service;
@Service
public class AdditionService {
public int add(int a, int b) {
return a + b;
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.testexecutionlisteners;
import static org.junit.Assert.assertThat;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = AdditionService.class)
public class AdditionServiceUnitTest {
@Autowired
private AdditionService additionService;
@Test
public void whenValidNumbersPassed_thenReturnSum() {
assertThat(additionService.add(5, 13), Matchers.is(18));
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.testexecutionlisteners;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
public class CustomTestExecutionListener implements TestExecutionListener, Ordered {
private static final Logger logger = LoggerFactory.getLogger(CustomTestExecutionListener.class);
public void beforeTestClass(TestContext testContext) throws Exception {
logger.info("beforeTestClass : {}", testContext.getTestClass());
};
public void prepareTestInstance(TestContext testContext) throws Exception {
logger.info("prepareTestInstance : {}", testContext.getTestClass());
};
public void beforeTestMethod(TestContext testContext) throws Exception {
logger.info("beforeTestMethod : {}", testContext.getTestMethod());
};
public void afterTestMethod(TestContext testContext) throws Exception {
logger.info("afterTestMethod : {}", testContext.getTestMethod());
};
public void afterTestClass(TestContext testContext) throws Exception {
logger.info("afterTestClass : {}", testContext.getTestClass());
}
@Override
public int getOrder() {
return Integer.MAX_VALUE;
};
}

View File

@ -0,0 +1,26 @@
package com.baeldung.testexecutionlisteners;
import static org.junit.Assert.assertThat;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.TestExecutionListeners.MergeMode;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@TestExecutionListeners(value = { CustomTestExecutionListener.class },
mergeMode = MergeMode.MERGE_WITH_DEFAULTS)
@ContextConfiguration(classes = AdditionService.class)
public class TestExecutionListenersWithMergeModeUnitTest {
@Autowired
private AdditionService additionService;
@Test
public void whenValidNumbersPassed_thenReturnSum() {
assertThat(additionService.add(5, 13), Matchers.is(18));
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.testexecutionlisteners;
import static org.junit.Assert.assertThat;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
@RunWith(SpringRunner.class)
@TestExecutionListeners(value = {CustomTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class})
@ContextConfiguration(classes = AdditionService.class)
public class TestExecutionListenersWithoutMergeModeUnitTest {
@Autowired
private AdditionService additionService;
@Test
public void whenValidNumbersPassed_thenReturnSum() {
assertThat(additionService.add(5, 13), Matchers.is(18));
}
}

View File

@ -0,0 +1,3 @@
# Test Execution Listeners
org.springframework.test.context.TestExecutionListener=\
com.baeldung.testexecutionlisteners.CustomTestExecutionListener