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.
2929import org .mockito .junit .jupiter .MockitoExtension ;
3030
3131import org .springframework .context .ApplicationContext ;
32- import org .springframework .core . ResolvableType ;
32+ import org .springframework .context . support . GenericApplicationContext ;
3333import org .springframework .http .HttpHeaders ;
3434import org .springframework .security .test .web .reactive .server .WebTestClientBuilder ;
3535import 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