Skip to content

Commit b2bac14

Browse files
committed
added tests
1 parent 5f6dc52 commit b2bac14

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

csp-spring-security/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
<artifactId>spring-boot-starter-test</artifactId>
4343
<scope>test</scope>
4444
</dependency>
45+
<dependency>
46+
<groupId>org.springframework.security</groupId>
47+
<artifactId>spring-security-test</artifactId>
48+
<scope>test</scope>
49+
</dependency>
4550
</dependencies>
4651

4752
<build>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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.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.web.servlet.MockMvc;
25+
26+
import static org.hamcrest.Matchers.*;
27+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
28+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
29+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
31+
32+
@WebMvcTest(controllers = GreetingController.class)
33+
class GreetingControllerTest {
34+
@Autowired
35+
private MockMvc mockMvc;
36+
37+
@Test
38+
@WithMockUser
39+
void home_returnsIndexView() throws Exception {
40+
mockMvc.perform(get("/"))
41+
.andExpect(status().isOk())
42+
.andExpect(view().name("index"))
43+
.andExpect(model().attributeExists("greeting"))
44+
.andExpect(model().attribute("greeting", instanceOf(Greeting.class)));
45+
}
46+
47+
@Test
48+
@WithMockUser
49+
void greeting_returnsResultView() throws Exception {
50+
mockMvc.perform(post("/greeting")
51+
.with(csrf())
52+
.param("name", "TestUser"))
53+
.andExpect(status().isOk())
54+
.andExpect(view().name("result"))
55+
.andExpect(model().attributeExists("result"))
56+
.andExpect(model().attribute("result", instanceOf(Greeting.class)));
57+
}
58+
59+
@Test
60+
void home_unauthenticated_returnsUnauthorized() throws Exception {
61+
mockMvc.perform(get("/"))
62+
.andExpect(status().isUnauthorized());
63+
}
64+
65+
@Test
66+
void greeting_unauthenticated_returnsUnauthorized() throws Exception {
67+
mockMvc.perform(post("/greeting")
68+
.with(csrf())
69+
.param("name", "TestUser"))
70+
.andExpect(status().isUnauthorized());
71+
}
72+
}

0 commit comments

Comments
 (0)