[JAVA-8355] Move article code to spring-testing-2 module

This commit is contained in:
Haroon Khan
2021-11-25 19:51:32 +00:00
parent 56f87cb2eb
commit 6d5ae33f5b
7 changed files with 11 additions and 8 deletions

View File

@@ -7,4 +7,3 @@
- [Using SpringJUnit4ClassRunner with Parameterized](https://www.baeldung.com/springjunit4classrunner-parameterized)
- [Override Properties in Springs Tests](https://www.baeldung.com/spring-tests-override-properties)
- [A Quick Guide to @DirtiesContext](https://www.baeldung.com/spring-dirtiescontext)
- [The Spring TestExecutionListener](https://www.baeldung.com/spring-testexecutionlistener)

View File

@@ -1,10 +0,0 @@
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

@@ -1,22 +0,0 @@
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

@@ -1,36 +0,0 @@
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

@@ -1,26 +0,0 @@
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

@@ -1,26 +0,0 @@
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));
}
}