BAEL-20888 Move Spring Boot Testing to Spring Boot Modules
This commit is contained in:
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.autoconfig.exclude;
|
||||
|
||||
import com.baeldung.boot.Application;
|
||||
import io.restassured.RestAssured;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
@TestPropertySource(properties = "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration")
|
||||
public class ExcludeAutoConfig1IntegrationTest {
|
||||
|
||||
/**
|
||||
* Encapsulates the random port the test server is listening on.
|
||||
*/
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Test
|
||||
public void givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired() {
|
||||
int statusCode = RestAssured.get("http://localhost:" + port).statusCode();
|
||||
assertEquals(HttpStatus.OK.value(), statusCode);
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.autoconfig.exclude;
|
||||
|
||||
import com.baeldung.boot.Application;
|
||||
import io.restassured.RestAssured;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
@ActiveProfiles("test")
|
||||
public class ExcludeAutoConfig2IntegrationTest {
|
||||
|
||||
/**
|
||||
* Encapsulates the random port the test server is listening on.
|
||||
*/
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Test
|
||||
public void givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired() {
|
||||
int statusCode = RestAssured.get("http://localhost:" + port).statusCode();
|
||||
assertEquals(HttpStatus.OK.value(), statusCode);
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.autoconfig.exclude;
|
||||
|
||||
import com.baeldung.boot.Application;
|
||||
import io.restassured.RestAssured;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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.web.server.LocalServerPort;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
@EnableAutoConfiguration(exclude=SecurityAutoConfiguration.class)
|
||||
public class ExcludeAutoConfig3IntegrationTest {
|
||||
|
||||
/**
|
||||
* Encapsulates the random port the test server is listening on.
|
||||
*/
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Test
|
||||
public void givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired() {
|
||||
int statusCode = RestAssured.get("http://localhost:" + port).statusCode();
|
||||
assertEquals(HttpStatus.OK.value(), statusCode);
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.autoconfig.exclude;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = TestApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
public class ExcludeAutoConfig4IntegrationTest {
|
||||
|
||||
/**
|
||||
* Encapsulates the random port the test server is listening on.
|
||||
*/
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Test
|
||||
public void givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired() {
|
||||
int statusCode = RestAssured.get("http://localhost:" + port).statusCode();
|
||||
assertEquals(HttpStatus.OK.value(), statusCode);
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.autoconfig.exclude;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||
|
||||
@SpringBootApplication(scanBasePackages="com.baeldung.boot", exclude=SecurityAutoConfiguration.class)
|
||||
public class TestApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestApplication.class, args);
|
||||
}
|
||||
}
|
||||
+15
@@ -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() {
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.boot.autoconfig;
|
||||
|
||||
import com.baeldung.boot.Application;
|
||||
import io.restassured.RestAssured;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
public class AutoConfigIntegrationTest {
|
||||
|
||||
/**
|
||||
* Encapsulates the random port the test server is listening on.
|
||||
*/
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Test
|
||||
public void givenNoAuthentication_whenAccessHome_thenUnauthorized() {
|
||||
int statusCode = RestAssured.get("http://localhost:" + port).statusCode();
|
||||
assertEquals(HttpStatus.UNAUTHORIZED.value(), statusCode);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAuthentication_whenAccessHome_thenOK() {
|
||||
int statusCode = RestAssured.given().auth().basic("john", "123").get("http://localhost:" + port).statusCode();
|
||||
assertEquals(HttpStatus.OK.value(), statusCode);
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.boot.embeddedRedis;
|
||||
|
||||
import com.baeldung.boot.embeddedRedis.configuration.RedisProperties;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import redis.embedded.RedisServer;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
@TestConfiguration
|
||||
public class TestRedisConfiguration {
|
||||
|
||||
private final RedisServer redisServer;
|
||||
|
||||
public TestRedisConfiguration(final RedisProperties redisProperties) {
|
||||
this.redisServer = new RedisServer(redisProperties.getRedisPort());
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void postConstruct() {
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void preDestroy() {
|
||||
redisServer.stop();
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.boot.embeddedRedis.domain.repository;
|
||||
|
||||
import com.baeldung.boot.embeddedRedis.TestRedisConfiguration;
|
||||
import com.baeldung.boot.embeddedRedis.domain.User;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = TestRedisConfiguration.class)
|
||||
public class UserRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Test
|
||||
public void shouldSaveUser_toRedis() {
|
||||
final UUID id = UUID.randomUUID();
|
||||
final User user = new User(id, "name");
|
||||
|
||||
final User saved = userRepository.save(user);
|
||||
|
||||
assertNotNull(saved);
|
||||
}
|
||||
}
|
||||
+73
@@ -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.rule.OutputCapture;
|
||||
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_CLASS;
|
||||
|
||||
@DirtiesContext(classMode = AFTER_CLASS)
|
||||
@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 OutputCapture outputCapture = new OutputCapture();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
+73
@@ -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.rule.OutputCapture;
|
||||
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_CLASS;
|
||||
|
||||
@DirtiesContext(classMode = AFTER_CLASS)
|
||||
@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 OutputCapture outputCapture = new OutputCapture();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
+73
@@ -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.rule.OutputCapture;
|
||||
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_CLASS;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DirtiesContext(classMode = AFTER_CLASS)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestLogLevelApplication.class)
|
||||
@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)
|
||||
@ActiveProfiles("logging-test")
|
||||
public class TestLogLevelWithProfileIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Rule
|
||||
public OutputCapture outputCapture = new OutputCapture();
|
||||
|
||||
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