我为我的控制器的get方法编写测试并创建自定义配置以进行身份验证但是在运行测试时,生成了默认密码并出现此错误:
MockHttpServletRequest:
HTTP Method = GET
Request URI = /android-guard-infos/1
Parameters = {}
Headers = {}
Body = <no character encoding set>
Session Attrs = {SPRING_SECURITY_SAVED_REQUEST=DefaultSavedRequest[http://localhost/android-guard-infos/1]}
Handler:
Type = null
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 401
Error message = Unauthorized
Headers = {WWW-Authenticate=[Basic realm="Realm"], X-Content- Type-Options=[nosniff], X-XSS-Protection=[1; mode=block], Cache-Control=[no-cache, no-store, max-age=0, must-revalidate], Pragma=[no-cache], Expires=[0], X-Frame-Options=[DENY]}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
java.lang.AssertionError: Status
Expected :200
Actual :401
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:55)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:82)
at org.springframework.test.web.servlet.result.StatusResultMatchers.lambda$matcher$9(StatusResultMatchers.java:619)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:178)
at ir.fidar.seclab.controllersTest.AndroidGuardInfoControllerTest.getOneTest(AndroidGuardInfoControllerTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
我对测试和安全的依赖是:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
和我的安全配置类:
@Configuration
@EnableWebSecurity
public class AppSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AppUserDetailsService appUserDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(getProvider());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and().authorizeRequests()
.antMatchers("/users/**","/roles/**","/privileges/**").hasAuthority("manager")
.antMatchers("/android-guard-infos/**","process-queues/**",
"/risk-android-infos/**","/summery-infos/**",
"/virus-total-infos/**","/vt-process-queues/**").permitAll();
}
@Bean
public AppAuthenticationProvider getProvider(){
AppAuthenticationProvider provider = new AppAuthenticationProvider(appUserDetailsService);
return provider;
}
}
我的考试班:
@RunWith(SpringRunner.class)
@WebMvcTest(AndroidGuardInfoController.class)
//@Import(AppSecurityConfig.class)
public class AndroidGuardInfoControllerTest {
@Autowired
private MockMvc mvc;
@MockBean
private AndroidGuardInfoService controller;
@Test
public void getOneTest() throws Exception {
AndroidGuardInfo data = new AndroidGuardInfo();
Mockito.when(controller.getOne(1L)).thenReturn(data);
mvc.perform(MockMvcRequestBuilders.get("/android-guard-infos/1")).andExpect(status().isOk());
}
}
我尝试通过添加@Import(AppSecurityConfig.class)来解决此错误,但将错误更改为java.lang.IllegalStateException: Failed to load ApplicationContext
引起的Error creating bean
。
你能指导我吗?
你的@Import(AppSecurityConfig.class)
在下面的bean依赖注入失败了。预期。
@Autowired
private AppUserDetailsService appUserDetailsService;
这是因为您尝试访问默认情况下指向src包的配置。
为了在test
目录中使用相同的配置(在/ src范围之外),您可以在AppSecurityConfig.java
中显式指定包扫描,组件扫描定义,或尝试添加测试特定配置,您可以使用包定义来定义测试级别配置。
@ComponentScan(basePackages = {
"YOUR_PACKAGES"
})
@Configuration
@EnableWebSecurity
public class TestAppSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AppUserDetailsService appUserDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(getProvider());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and().authorizeRequests()
.antMatchers("/users/**","/roles/**","/privileges/**").hasAuthority("manager")
.antMatchers("/android-guard-infos/**","process-queues/**",
"/risk-android-infos/**","/summery-infos/**",
"/virus-total-infos/**","/vt-process-queues/**").permitAll();
}
@Bean
public AppAuthenticationProvider getProvider(){
AppAuthenticationProvider provider = new AppAuthenticationProvider(appUserDetailsService);
return provider;
}
}
然后尝试在您的测试类中导入它。
@RunWith(SpringRunner.class)
@WebMvcTest(AndroidGuardInfoController.class)
@Import(TestAppSecurityConfig.class)
public class AndroidGuardInfoControllerTest {