Skip to content

Commit c0d4eee

Browse files
committed
configure live tests
1 parent 82065d8 commit c0d4eee

3 files changed

Lines changed: 65 additions & 9 deletions

File tree

‎spring-security-custom-permission/pom.xml‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@
5555
</dependency>
5656

5757
<!-- -->
58-
58+
<dependency>
59+
<groupId>org.springframework.boot</groupId>
60+
<artifactId>spring-boot-starter-test</artifactId>
61+
<scope>test</scope>
62+
</dependency>
63+
5964
<dependency>
6065
<groupId>junit</groupId>
6166
<artifactId>junit</artifactId>
@@ -95,13 +100,54 @@
95100
<groupId>org.springframework.boot</groupId>
96101
<artifactId>spring-boot-maven-plugin</artifactId>
97102
</plugin>
103+
104+
<plugin>
105+
<artifactId>maven-surefire-plugin</artifactId>
106+
<configuration>
107+
<skipTests>${unit-tests.skip}</skipTests>
108+
<excludes>
109+
<exclude>**/*LiveTest.java</exclude>
110+
</excludes>
111+
</configuration>
112+
</plugin>
113+
114+
<plugin>
115+
<artifactId>maven-failsafe-plugin</artifactId>
116+
<executions>
117+
<execution>
118+
<id>integration-test</id>
119+
<goals>
120+
<goal>integration-test</goal>
121+
</goals>
122+
<configuration>
123+
<skipTests>${live-tests.skip}</skipTests>
124+
<includes>
125+
<include>**/*LiveTest.class</include>
126+
</includes>
127+
</configuration>
128+
</execution>
129+
</executions>
130+
</plugin>
131+
98132
</plugins>
99133
</build>
100134

135+
<profiles>
136+
<profile>
137+
<id>live</id>
138+
<properties>
139+
<unit-tests.skip>false</unit-tests.skip>
140+
<live-tests.skip>false</live-tests.skip>
141+
</properties>
142+
</profile>
143+
</profiles>
144+
101145
<properties>
102146
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
103147
<java.version>1.8</java.version>
104148
<rest-assured.version>2.4.0</rest-assured.version>
149+
<unit-tests.skip>false</unit-tests.skip>
150+
<live-tests.skip>true</live-tests.skip>
105151
</properties>
106152

107153
</project>

‎spring-security-custom-permission/src/main/resources/application.properties‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
server.port=8081
22
spring.datasource.driver-class-name=org.h2.Driver
3-
spring.datasource.url=jdbc:h2:mem:security_permission;DB_CLOSE_DELAY=-1
3+
spring.datasource.url=jdbc:h2:mem:security_permission;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
44
spring.datasource.username=sa
55
spring.datasource.password=
66
spring.jpa.hibernate.ddl-auto=create-drop

‎spring-security-custom-permission/src/test/java/org/baeldung/web/LiveTest.java‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,45 @@
33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertTrue;
55

6+
import org.baeldung.Application;
67
import org.baeldung.persistence.model.Foo;
78
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.boot.test.IntegrationTest;
11+
import org.springframework.boot.test.SpringApplicationConfiguration;
812
import org.springframework.http.MediaType;
13+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14+
import org.springframework.test.context.web.WebAppConfiguration;
915

1016
import com.jayway.restassured.RestAssured;
1117
import com.jayway.restassured.authentication.FormAuthConfig;
1218
import com.jayway.restassured.response.Response;
1319
import com.jayway.restassured.specification.RequestSpecification;
1420

21+
@RunWith(SpringJUnit4ClassRunner.class)
22+
@SpringApplicationConfiguration(classes = Application.class)
23+
@WebAppConfiguration
24+
@IntegrationTest("server.port:8082")
1525
public class LiveTest {
1626

17-
private final FormAuthConfig formAuthConfig = new FormAuthConfig("http://localhost:8081/login", "username", "password");
27+
private final FormAuthConfig formAuthConfig = new FormAuthConfig("http://localhost:8082/login", "username", "password");
1828

1929
@Test
2030
public void givenUserWithReadPrivilegeAndHasPermission_whenGetFooById_thenOK() {
21-
final Response response = givenAuth("john", "123").get("http://localhost:8081/foos/1");
31+
final Response response = givenAuth("john", "123").get("http://localhost:8082/foos/1");
2232
assertEquals(200, response.getStatusCode());
2333
assertTrue(response.asString().contains("id"));
2434
}
2535

2636
@Test
2737
public void givenUserWithNoWritePrivilegeAndHasPermission_whenPostFoo_thenForbidden() {
28-
final Response response = givenAuth("john", "123").contentType(MediaType.APPLICATION_JSON_VALUE).body(new Foo("sample")).post("http://localhost:8081/foos");
38+
final Response response = givenAuth("john", "123").contentType(MediaType.APPLICATION_JSON_VALUE).body(new Foo("sample")).post("http://localhost:8082/foos");
2939
assertEquals(403, response.getStatusCode());
3040
}
3141

3242
@Test
3343
public void givenUserWithWritePrivilegeAndHasPermission_whenPostFoo_thenOk() {
34-
final Response response = givenAuth("tom", "111").contentType(MediaType.APPLICATION_JSON_VALUE).body(new Foo("sample")).post("http://localhost:8081/foos");
44+
final Response response = givenAuth("tom", "111").contentType(MediaType.APPLICATION_JSON_VALUE).body(new Foo("sample")).post("http://localhost:8082/foos");
3545
assertEquals(201, response.getStatusCode());
3646
assertTrue(response.asString().contains("id"));
3747
}
@@ -40,22 +50,22 @@ public void givenUserWithWritePrivilegeAndHasPermission_whenPostFoo_thenOk() {
4050

4151
@Test
4252
public void givenUserMemberInOrganization_whenGetOrganization_thenOK() {
43-
final Response response = givenAuth("john", "123").get("http://localhost:8081/organizations/1");
53+
final Response response = givenAuth("john", "123").get("http://localhost:8082/organizations/1");
4454
assertEquals(200, response.getStatusCode());
4555
assertTrue(response.asString().contains("id"));
4656
}
4757

4858
@Test
4959
public void givenUserMemberNotInOrganization_whenGetOrganization_thenForbidden() {
50-
final Response response = givenAuth("john", "123").get("http://localhost:8081/organizations/2");
60+
final Response response = givenAuth("john", "123").get("http://localhost:8082/organizations/2");
5161
assertEquals(403, response.getStatusCode());
5262
}
5363

5464
//
5565

5666
@Test
5767
public void givenDisabledSecurityExpression_whenGetFooByName_thenError() {
58-
final Response response = givenAuth("john", "123").get("http://localhost:8081/foos?name=sample");
68+
final Response response = givenAuth("john", "123").get("http://localhost:8082/foos?name=sample");
5969
assertEquals(500, response.getStatusCode());
6070
assertTrue(response.asString().contains("method hasAuthority() not allowed"));
6171
}

0 commit comments

Comments
 (0)