Skip to content

Commit 1ca5ff4

Browse files
committed
added tests
1 parent da6a2be commit 1ca5ff4

5 files changed

Lines changed: 394 additions & 0 deletions

File tree

session-handling-spring-security/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
<artifactId>spring-boot-starter-test</artifactId>
6363
<scope>test</scope>
6464
</dependency>
65+
<dependency>
66+
<groupId>org.springframework.security</groupId>
67+
<artifactId>spring-security-test</artifactId>
68+
<scope>test</scope>
69+
</dependency>
6570
</dependencies>
6671

6772
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (C) 2026 Dominik Schadow, dominikschadow@gmail.com
3+
*
4+
* This file is part of the Java Security project.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package de.dominikschadow.javasecurity.sessionhandling.greetings;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
23+
import org.springframework.security.test.context.support.WithMockUser;
24+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
25+
import org.springframework.test.web.servlet.MockMvc;
26+
27+
import static org.mockito.Mockito.when;
28+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
30+
31+
@WebMvcTest(GreetingController.class)
32+
class GreetingControllerTest {
33+
34+
@Autowired
35+
private MockMvc mockMvc;
36+
37+
@MockitoBean
38+
private GreetingService greetingService;
39+
40+
@Test
41+
@WithMockUser
42+
void index_shouldReturnIndexView() throws Exception {
43+
mockMvc.perform(get("/"))
44+
.andExpect(status().isOk())
45+
.andExpect(view().name("index"))
46+
.andExpect(model().attributeExists("sessionId"));
47+
}
48+
49+
@Test
50+
@WithMockUser(roles = "USER")
51+
void greetUser_shouldReturnUserViewWithGreeting() throws Exception {
52+
when(greetingService.greetUser()).thenReturn("Hello User!");
53+
54+
mockMvc.perform(get("/user/user"))
55+
.andExpect(status().isOk())
56+
.andExpect(view().name("user/user"))
57+
.andExpect(model().attributeExists("sessionId"))
58+
.andExpect(model().attribute("greeting", "Hello User!"));
59+
}
60+
61+
@Test
62+
@WithMockUser(roles = "ADMIN")
63+
void greetAdmin_shouldReturnAdminViewWithGreeting() throws Exception {
64+
when(greetingService.greetAdmin()).thenReturn("Hello Admin!");
65+
66+
mockMvc.perform(get("/admin/admin"))
67+
.andExpect(status().isOk())
68+
.andExpect(view().name("admin/admin"))
69+
.andExpect(model().attributeExists("sessionId"))
70+
.andExpect(model().attribute("greeting", "Hello Admin!"));
71+
}
72+
73+
@Test
74+
void index_withoutAuthentication_shouldReturnUnauthorized() throws Exception {
75+
mockMvc.perform(get("/"))
76+
.andExpect(status().isUnauthorized());
77+
}
78+
79+
@Test
80+
void greetUser_withoutAuthentication_shouldReturnUnauthorized() throws Exception {
81+
mockMvc.perform(get("/user/user"))
82+
.andExpect(status().isUnauthorized());
83+
}
84+
85+
@Test
86+
void greetAdmin_withoutAuthentication_shouldReturnUnauthorized() throws Exception {
87+
mockMvc.perform(get("/admin/admin"))
88+
.andExpect(status().isUnauthorized());
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (C) 2026 Dominik Schadow, dominikschadow@gmail.com
3+
*
4+
* This file is part of the Java Security project.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package de.dominikschadow.javasecurity.sessionhandling.greetings;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.context.SpringBootTest;
23+
import org.springframework.security.access.AccessDeniedException;
24+
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
25+
import org.springframework.security.test.context.support.WithMockUser;
26+
27+
import static org.junit.jupiter.api.Assertions.*;
28+
29+
@SpringBootTest
30+
class GreetingServiceTest {
31+
32+
@Autowired
33+
private GreetingService greetingService;
34+
35+
@Test
36+
@WithMockUser(roles = "USER")
37+
void greetUser_withUserRole_shouldReturnGreeting() {
38+
String greeting = greetingService.greetUser();
39+
40+
assertEquals("Spring Security says hello to the user!", greeting);
41+
}
42+
43+
@Test
44+
@WithMockUser(roles = "ADMIN")
45+
void greetUser_withAdminRole_shouldReturnGreeting() {
46+
String greeting = greetingService.greetUser();
47+
48+
assertEquals("Spring Security says hello to the user!", greeting);
49+
}
50+
51+
@Test
52+
@WithMockUser(roles = "ADMIN")
53+
void greetAdmin_withAdminRole_shouldReturnGreeting() {
54+
String greeting = greetingService.greetAdmin();
55+
56+
assertEquals("Spring Security says hello to the admin!", greeting);
57+
}
58+
59+
@Test
60+
@WithMockUser(roles = "USER")
61+
void greetAdmin_withUserRole_shouldThrowAccessDeniedException() {
62+
assertThrows(AccessDeniedException.class, () -> greetingService.greetAdmin());
63+
}
64+
65+
@Test
66+
void greetUser_withoutAuthentication_shouldThrowAuthenticationCredentialsNotFoundException() {
67+
assertThrows(AuthenticationCredentialsNotFoundException.class, () -> greetingService.greetUser());
68+
}
69+
70+
@Test
71+
void greetAdmin_withoutAuthentication_shouldThrowAuthenticationCredentialsNotFoundException() {
72+
assertThrows(AuthenticationCredentialsNotFoundException.class, () -> greetingService.greetAdmin());
73+
}
74+
}

session-handling/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
<groupId>javax.servlet</groupId>
2323
<artifactId>javax.servlet-api</artifactId>
2424
</dependency>
25+
<dependency>
26+
<groupId>org.junit.jupiter</groupId>
27+
<artifactId>junit-jupiter</artifactId>
28+
<scope>test</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.mockito</groupId>
32+
<artifactId>mockito-core</artifactId>
33+
<scope>test</scope>
34+
</dependency>
2535
</dependencies>
2636

2737
<build>

0 commit comments

Comments
 (0)