[JAVA-8377] Split spring-boot-testing module
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.boot;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.testloglevel;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.test.system.OutputCaptureRule;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD;
|
||||
|
||||
@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD)
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestLogLevelApplication.class)
|
||||
@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)
|
||||
@ActiveProfiles("logback-test2")
|
||||
public class LogbackMultiProfileTestLogLevelIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Rule
|
||||
public OutputCaptureRule outputCapture = new OutputCaptureRule();
|
||||
|
||||
private String baseUrl = "/testLogLevel";
|
||||
|
||||
@Test
|
||||
public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenPrintTraceLogsForOurPackage() {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(baseUrl, String.class);
|
||||
|
||||
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||
assertThatOutputContainsLogForOurPackage("TRACE");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenNoTraceLogsForOtherPackages() {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(baseUrl, String.class);
|
||||
|
||||
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||
assertThatOutputDoesntContainLogForOtherPackages("TRACE");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenPrintErrorLogs() {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(baseUrl, String.class);
|
||||
|
||||
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||
assertThatOutputContainsLogForOurPackage("ERROR");
|
||||
assertThatOutputContainsLogForOtherPackages("ERROR");
|
||||
}
|
||||
|
||||
private void assertThatOutputContainsLogForOurPackage(String level) {
|
||||
assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*");
|
||||
}
|
||||
|
||||
private void assertThatOutputDoesntContainLogForOtherPackages(String level) {
|
||||
assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level);
|
||||
}
|
||||
|
||||
private void assertThatOutputContainsLogForOtherPackages(String level) {
|
||||
assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.testloglevel;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.test.system.OutputCaptureRule;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD;
|
||||
|
||||
@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD)
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestLogLevelApplication.class)
|
||||
@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)
|
||||
@ActiveProfiles("logback-test")
|
||||
public class LogbackTestLogLevelIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Rule
|
||||
public OutputCaptureRule outputCapture = new OutputCaptureRule();
|
||||
|
||||
private String baseUrl = "/testLogLevel";
|
||||
|
||||
@Test
|
||||
public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintDebugLogsForOurPackage() {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(baseUrl, String.class);
|
||||
|
||||
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||
assertThatOutputContainsLogForOurPackage("DEBUG");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(baseUrl, String.class);
|
||||
|
||||
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||
assertThatOutputDoesntContainLogForOtherPackages("DEBUG");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintErrorLogs() {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(baseUrl, String.class);
|
||||
|
||||
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||
assertThatOutputContainsLogForOurPackage("ERROR");
|
||||
assertThatOutputContainsLogForOtherPackages("ERROR");
|
||||
}
|
||||
|
||||
private void assertThatOutputContainsLogForOurPackage(String level) {
|
||||
assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*");
|
||||
}
|
||||
|
||||
private void assertThatOutputDoesntContainLogForOtherPackages(String level) {
|
||||
assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level);
|
||||
}
|
||||
|
||||
private void assertThatOutputContainsLogForOtherPackages(String level) {
|
||||
assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.testloglevel;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.test.system.OutputCaptureRule;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestLogLevelApplication.class)
|
||||
@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)
|
||||
@ActiveProfiles("logging-test")
|
||||
public class TestLogLevelWithProfileIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Rule
|
||||
public OutputCaptureRule outputCapture = new OutputCaptureRule();
|
||||
|
||||
private String baseUrl = "/testLogLevel";
|
||||
|
||||
@Test
|
||||
public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintDebugLogsForOurPackage() {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(baseUrl, String.class);
|
||||
|
||||
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||
assertThatOutputContainsLogForOurPackage("DEBUG");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(baseUrl, String.class);
|
||||
|
||||
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||
assertThatOutputDoesntContainLogForOtherPackages("DEBUG");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintInfoLogs() {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(baseUrl, String.class);
|
||||
|
||||
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||
assertThatOutputContainsLogForOurPackage("INFO");
|
||||
assertThatOutputContainsLogForOtherPackages("INFO");
|
||||
}
|
||||
|
||||
private void assertThatOutputContainsLogForOurPackage(String level) {
|
||||
assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*");
|
||||
}
|
||||
|
||||
private void assertThatOutputDoesntContainLogForOtherPackages(String level) {
|
||||
assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level);
|
||||
}
|
||||
|
||||
private void assertThatOutputContainsLogForOtherPackages(String level) {
|
||||
assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user