[英]Spring MVC Test: java.lang.AssertionError: No ModelAndView found
[英]JUnit Test a Controller View with Spring Security throws java.lang.AssertionError: No ModelAndView found after HTTP Status 302
我对 Spring 测试完全陌生,我不知道我在这里缺少什么。 我有下面的 controller class 我想测试。 我也有 Spring Security 并且我已经从 antMatchers 中排除了 controller 以便重定向到登录页面。 我在下面写了测试,但我收到了这个问题标题的消息。 我应该怎么办?
Controller 测试
@Autowired
private MockMvc mockMvc;
@Test
@DisplayName("Testing the Verification Complete view of the application")
public void testVerificationCompletePage() throws Exception{
mockMvc.perform(get("/verification-complete"))
.andExpect(status().is3xxRedirection())
.andExpect(view().name("login"));
}
Controller Class
@GetMapping("/verification-complete")
public String showVerificationCompleteForm(){
return "verification-complete";
}
Spring 安全配置
package com.andrekreou.iot.authentication.security;
import com.andrekreou.iot.authentication.user.ApplicationUserService;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@AllArgsConstructor
@EnableWebSecurity
public class ApplicationSecurityConfig {
private final ApplicationUserService applicationUserService;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
@Bean
protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf().disable()
.requiresChannel()
.antMatchers("/actuator/prometheus")
.requiresInsecure()
.and()
.authorizeRequests()
.antMatchers(
"/api/v*/registration/**",
"/register*",
"/login",
"/registration",
"/registration-complete",
"/actuator/prometheus").permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/login")
.usernameParameter("email")
.permitAll()
.defaultSuccessUrl("/",true)
.failureUrl("/login-error")
.and()
.logout()
.logoutUrl("/logout")
.clearAuthentication(true)
.invalidateHttpSession(true)
.deleteCookies("JSESSIONID","Idea-2e8e7cee")
.logoutSuccessUrl("/login");
return http.build();
}
我相信您应该使用内置方法进行检查:
mockMvc.perform(get("/verification-complete"))
.andExpect(redirectedUrl("/login"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.