[JAVA-8355] Move article code to spring-testing-2 module
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.testexecutionlisteners;
|
||||
|
||||
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;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
@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));
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.testexecutionlisteners;
|
||||
|
||||
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;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
@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));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.testexecutionlisteners;
|
||||
|
||||
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;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
@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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user