Skip to content

Commit 492ddc9

Browse files
committed
Add WebMvcTestOAuth2Tests
Signed-off-by: Dmytro Nosan <[email protected]>
1 parent cd6b0fe commit 492ddc9

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

spring-boot-project/spring-boot-test-autoconfigure/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ dependencies {
110110
testImplementation("org.mockito:mockito-junit-jupiter")
111111
testImplementation("org.skyscreamer:jsonassert")
112112
testImplementation("org.springframework:spring-core-test")
113+
testImplementation("org.springframework.boot:spring-boot-starter-oauth2-client")
113114
testImplementation("org.springframework.hateoas:spring-hateoas")
114115
testImplementation("org.springframework.plugin:spring-plugin-core")
115116
testImplementation("org.thymeleaf:thymeleaf")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.web.servlet.mockmvc;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
23+
import org.springframework.test.web.servlet.MockMvc;
24+
25+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
26+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern;
27+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
28+
29+
/**
30+
* Tests for {@link WebMvcTest @WebMvcTest} with OAuth2.
31+
*
32+
* @author Dmytro Nosan
33+
*/
34+
@WebMvcTest(controllers = ExampleController1.class,
35+
properties = { "spring.security.oauth2.client.registration.test.client-id=test",
36+
"spring.security.oauth2.client.registration.test.authorization-grant-type=authorization-code",
37+
"spring.security.oauth2.client.provider.test.authorization-uri=https://auth.example.org" })
38+
class WebMvcTestOAuth2Tests {
39+
40+
@Autowired
41+
private MockMvc mockMvc;
42+
43+
@Test
44+
void loginShouldBeAuthorized() throws Exception {
45+
this.mockMvc.perform(get("/one")).andExpect(status().isFound()).andExpect(redirectedUrlPattern("**/login"));
46+
}
47+
48+
}

0 commit comments

Comments
 (0)