Solucionado (ver solução)
Solucionado
(ver solução)
4
respostas

java.lang.IllegalStateException: Failed to load ApplicationContext

Erro ao testar a classe ProdutosControllerTest, já tentei a solução do topico: https://cursos.alura.com.br/forum/topico-java-lang-illegalstateexception-failed-to-load-applicationcontext-63858

Mas não deu certo, esse tipo de coisa da um desanimo, muitas horas pra tentar descobrir um erro.

package br.com.casadocodigo.loja.controllers;

import javax.servlet.Filter;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import br.com.casadocodigo.loja.conf.AppWebConfiguration;
import br.com.casadocodigo.loja.conf.DataSourceConfigurationTest;
import br.com.casadocodigo.loja.conf.JPAConfiguration;
import br.com.casadocodigo.loja.conf.SecurityConfiguration;

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {JPAConfiguration.class, AppWebConfiguration.class, 
    DataSourceConfigurationTest.class, SecurityConfiguration.class})
@ActiveProfiles("test")
public class ProdutosControllerTest {

    @Autowired
    private WebApplicationContext wac;

    @Autowired
    private Filter springSecurityFilterChain;

    private MockMvc mockMvc;

    @Before
    public void setup(){
        this.mockMvc = MockMvcBuilders
                        .webAppContextSetup(wac)
                        .addFilter(springSecurityFilterChain)
                        .build();
    }

    @Test
    public void deveRetornarParaHomeComOsLivros() 
            throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/"))
            .andExpect(MockMvcResultMatchers
                .model().attributeExists("produtos"))
            .andExpect(MockMvcResultMatchers
                .forwardedUrl("/WEB-INF/views/home.jsp"));
    }

    @Test
    public void somenteAdminDeveAcessarProdutosForm() 
            throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/produtos/form")
            .with(SecurityMockMvcRequestPostProcessors
                .user("user@casadocodigo.com.br").password("123456")
                .roles("USUARIO")))
            .andExpect(MockMvcResultMatchers.status().is(403));
    }

}

Exception: https://gist.github.com/dteodoro/c748956f1c71b52d22fe47057949a937

4 respostas

Oi Dario,

Posta aqui o stack do erro, por favor.

Abraço!

Oi Otávio, Ficou muito grande e postei no link do GIT.

Exception: https://gist.github.com/dteodoro/c748956f1c71b52d22fe47057949a937

Abraço!

solução!

Resolvi o problema alterando a versão do JDK. do jdk-11-0.2 para jdk1.8.0_202