Home > Software design >  JUnit Test a Controller View with Spring Security throws java.lang.AssertionError: No ModelAndView f
JUnit Test a Controller View with Spring Security throws java.lang.AssertionError: No ModelAndView f

Time:10-10

I am totally new to Spring Testing and I don't know what I am missing here. I have the below controller class that I want to test. I have also Spring Security and I have excluded that controller from antMatchers in order to get redirection to the login page. I wrote the test below but I am receiving the message of the title of this question. What should I do?

Controller Test

    @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 Security Config

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();
    }

CodePudding user response:

You should check with built in method I believe:

mockMvc.perform(get("/verification-complete"))
            .andExpect(redirectedUrl("/login"));
  • Related