Skip to content

Commit a22daf9

Browse files
author
Ambrus Adrián-Zoltán
committed
Added test cases
1 parent 6e9f4a6 commit a22daf9

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<properties>
2222
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2323
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<rest-assured.version>3.0.1</rest-assured.version>
2425
<java.version>1.8</java.version>
2526
</properties>
2627

@@ -43,6 +44,45 @@
4344
<artifactId>spring-boot-starter-test</artifactId>
4445
<scope>test</scope>
4546
</dependency>
47+
48+
<!-- -->
49+
50+
<dependency>
51+
<groupId>junit</groupId>
52+
<artifactId>junit</artifactId>
53+
<scope>test</scope>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>org.hamcrest</groupId>
58+
<artifactId>hamcrest-core</artifactId>
59+
<scope>test</scope>
60+
</dependency>
61+
62+
<dependency>
63+
<groupId>org.hamcrest</groupId>
64+
<artifactId>hamcrest-library</artifactId>
65+
<scope>test</scope>
66+
</dependency>
67+
68+
<dependency>
69+
<groupId>org.springframework</groupId>
70+
<artifactId>spring-test</artifactId>
71+
<version>4.3.3.RELEASE</version>
72+
</dependency>
73+
74+
<dependency>
75+
<groupId>org.springframework.security</groupId>
76+
<artifactId>spring-security-test</artifactId>
77+
<version>4.1.3.RELEASE</version>
78+
<scope>test</scope>
79+
</dependency>
80+
81+
<dependency>
82+
<groupId>org.springframework.security</groupId>
83+
<artifactId>spring-security-web</artifactId>
84+
<version>4.1.3.RELEASE</version>
85+
</dependency>
4686
</dependencies>
4787

4888
<build>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.baeldung.web;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.test.context.ContextConfiguration;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
import org.springframework.test.context.web.WebAppConfiguration;
11+
import org.springframework.test.web.servlet.MockMvc;
12+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
13+
import org.springframework.web.context.WebApplicationContext;
14+
15+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
16+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
17+
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
18+
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
19+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
20+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
21+
22+
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @WebAppConfiguration @SpringBootTest public class LiveTest {
23+
@Autowired private WebApplicationContext context;
24+
25+
private MockMvc mvc;
26+
27+
@Before public void setup() {
28+
mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
29+
}
30+
31+
@Test public void givenUnauthenticatedUser_whenAccessingMainPage_thenRedirect() throws Exception {
32+
mvc.perform(get("/")).andExpect(status().is3xxRedirection());
33+
}
34+
35+
@Test public void givenValidUsernameAndPassword_whenLogin_thenOK() throws Exception {
36+
mvc.perform(formLogin("/login").user("user").password("pass")).andExpect(authenticated());
37+
}
38+
39+
@Test public void givenAuthenticatedAdmin_whenAccessingMainPage_thenOK() throws Exception {
40+
mvc.perform(get("/").with(user("admin").password("pass").roles("ADMIN"))).andExpect(status().isOk());
41+
}
42+
}

0 commit comments

Comments
 (0)