Skip to content

Commit 9dda65a

Browse files
ngocnhan-tran1996jzheaux
authored andcommitted
Polish CorsSpecTests
Use concrete ApplicationContext to simplify future maintenance. Issue gh-4832
1 parent 68d814e commit 9dda65a

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

config/src/test/java/org/springframework/security/config/web/server/CorsSpecTests.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,7 +29,7 @@
2929
import org.mockito.junit.jupiter.MockitoExtension;
3030

3131
import org.springframework.context.ApplicationContext;
32-
import org.springframework.core.ResolvableType;
32+
import org.springframework.context.support.GenericApplicationContext;
3333
import org.springframework.http.HttpHeaders;
3434
import org.springframework.security.test.web.reactive.server.WebTestClientBuilder;
3535
import org.springframework.test.web.reactive.server.FluxExchangeResult;
@@ -51,7 +51,6 @@ public class CorsSpecTests {
5151
@Mock
5252
private CorsConfigurationSource source;
5353

54-
@Mock
5554
private ApplicationContext context;
5655

5756
ServerHttpSecurity http;
@@ -62,6 +61,8 @@ public class CorsSpecTests {
6261

6362
@BeforeEach
6463
public void setup() {
64+
this.context = new GenericApplicationContext();
65+
((GenericApplicationContext) this.context).refresh();
6566
this.http = new TestingServerHttpSecurity().applicationContext(this.context);
6667
}
6768

@@ -92,17 +93,14 @@ public void corsWhenEnabledInLambdaThenAccessControlAllowOriginAndSecurityHeader
9293
@Test
9394
public void corsWhenCorsConfigurationSourceBeanThenAccessControlAllowOriginAndSecurityHeaders() {
9495
givenGetCorsConfigurationWillReturnWildcard();
95-
given(this.context.getBeanNamesForType(any(ResolvableType.class))).willReturn(new String[] { "source" },
96-
new String[0]);
97-
given(this.context.getBean("source")).willReturn(this.source);
96+
((GenericApplicationContext) this.context).registerBean(CorsConfigurationSource.class, () -> this.source);
9897
this.expectedHeaders.set("Access-Control-Allow-Origin", "*");
9998
this.expectedHeaders.set("X-Frame-Options", "DENY");
10099
assertHeaders();
101100
}
102101

103102
@Test
104103
public void corsWhenNoConfigurationSourceThenNoCorsHeaders() {
105-
given(this.context.getBeanNamesForType(any(ResolvableType.class))).willReturn(new String[0]);
106104
this.headerNamesNotPresent.add("Access-Control-Allow-Origin");
107105
assertHeaders();
108106
}

0 commit comments

Comments
 (0)