Merge pull request #6036 from Doha2012/master

exclude auto-config classes
This commit is contained in:
Loredana Crusoveanu 2019-01-03 22:43:32 +02:00 committed by GitHub
commit 1695857b91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 202 additions and 48 deletions

View File

@ -23,6 +23,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
@ -43,6 +47,13 @@
<version>${spock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -98,51 +109,14 @@
</goals>
</execution>
</executions>
</plugin>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>autoconfiguration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>**/*LiveTest.java</exclude>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*IntTest.java</exclude>
</excludes>
<includes>
<include>**/AutoconfigurationTest.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<test.mime>json</test.mime>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<properties>
<!-- The main class to start by executing java -jar -->
<start-class>org.baeldung.boot.Application</start-class>
<start-class>com.baeldung.boot.Application</start-class>
<git-commit-id-plugin.version>2.2.4</git-commit-id-plugin.version>
<spock.version>1.2-groovy-2.4</spock.version>
</properties>

View File

@ -1,4 +1,4 @@
package org.baeldung.boot;
package com.baeldung.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

View File

@ -0,0 +1,14 @@
package com.baeldung.boot.controller.rest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@GetMapping("/")
public String salutation() {
return "Welcome !";
}
}

View File

@ -1,4 +1,6 @@
package org.baeldung.boot.controller.rest;
package com.baeldung.boot.controller.rest;
import java.util.Optional;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
@ -9,8 +11,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;
@RestController
@RequestMapping("/hello")
public class WebController {

View File

@ -0,0 +1,3 @@
# test properties
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration

View File

@ -0,0 +1,4 @@
# security
spring.security.user.name=john
spring.security.user.password=123

View File

@ -1,6 +1,6 @@
package org.baeldung.boot
package com.baeldung.boot
import org.baeldung.boot.controller.rest.WebController
import com.baeldung.boot.controller.rest.WebController
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import spock.lang.Narrative

View File

@ -1,4 +1,4 @@
package org.baeldung.boot
package com.baeldung.boot
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
@ -12,8 +12,8 @@ import spock.lang.Title
@Title("WebController Specification")
@Narrative("The Specification of the behaviour of the WebController. It can greet a person, change the name and reset it to 'world'")
@AutoConfigureMockMvc
@WebMvcTest
@AutoConfigureMockMvc(secure=false)
@WebMvcTest()
class WebControllerTest extends Specification {
@Autowired

View File

@ -0,0 +1,26 @@
package com.baeldung.autoconfig.exclude;
import static org.junit.Assert.assertEquals;
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.http.HttpStatus;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.boot.Application;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
@TestPropertySource(properties = "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration")
public class ExcludeAutoConfig1IntegrationTest {
@Test
public void givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired() {
int statusCode = RestAssured.get("http://localhost:8080/").statusCode();
assertEquals(HttpStatus.OK.value(), statusCode);
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.autoconfig.exclude;
import static org.junit.Assert.assertEquals;
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.http.HttpStatus;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.boot.Application;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
@ActiveProfiles("test")
public class ExcludeAutoConfig2IntegrationTest {
@Test
public void givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired() {
int statusCode = RestAssured.get("http://localhost:8080/").statusCode();
assertEquals(HttpStatus.OK.value(), statusCode);
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.autoconfig.exclude;
import static org.junit.Assert.assertEquals;
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.http.HttpStatus;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.boot.Application;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
@EnableAutoConfiguration(exclude=SecurityAutoConfiguration.class)
public class ExcludeAutoConfig3IntegrationTest {
@Test
public void givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired() {
int statusCode = RestAssured.get("http://localhost:8080/").statusCode();
assertEquals(HttpStatus.OK.value(), statusCode);
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.autoconfig.exclude;
import static org.junit.Assert.assertEquals;
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.http.HttpStatus;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT)
public class ExcludeAutoConfig4IntegrationTest {
@Test
public void givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired() {
int statusCode = RestAssured.get("http://localhost:8080/").statusCode();
assertEquals(HttpStatus.OK.value(), statusCode);
}
}

View File

@ -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);
}
}

View File

@ -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 SpringContextIntegrationTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.boot.autoconfig;
import static org.junit.Assert.assertEquals;
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.http.HttpStatus;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.boot.Application;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
public class AutoConfigIntegrationTest {
@Test
public void givenNoAuthentication_whenAccessHome_thenUnauthorized() {
int statusCode = RestAssured.get("http://localhost:8080/").statusCode();
assertEquals(HttpStatus.UNAUTHORIZED.value(), statusCode);
}
@Test
public void givenAuthentication_whenAccessHome_thenOK() {
int statusCode = RestAssured.given().auth().basic("john", "123").get("http://localhost:8080/").statusCode();
assertEquals(HttpStatus.OK.value(), statusCode);
}
}