Domanda

Sto provando a testare la mia applicazione con junit.

Pertanto ho impostato la seguente classe:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/META-INF/spring/applicationContext-test.xml" )
@TransactionConfiguration
@Transactional
public class DispatcherServletTest extends AbstractJUnit4SpringContextTests {

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;

    private DispatcherServlet dispatcher;

    @Before
    public void setUp() throws Exception {
            request = new MockHttpServletRequest();
            response = new MockHttpServletResponse();

            MockServletConfig config = new MockServletConfig("myapp");
            config.addInitParameter("contextConfigLocation","classpath*:webmvc-config.xml");

            dispatcher = new DispatcherServlet();
            dispatcher.init(config);
    }
    //test cases

}

Quindi il problema è che sembra che il mio servlet del dispatcher non possa inviare alcuna richiesta a nessuno dei miei controller.

Penso che ci sia qualcosa con la configurazione - contextConfigurationLocation. Sembra che riesca a trovare il file (altrimenti genererebbe un'eccezione), ma non carica alcuna configurazione

Il logger dice:

org.springframework.web.servlet.PageNotFound - Nessuna mappatura trovata per la richiesta HTTP con URI [http:// localhost: 8080 / myapp / abc]

Ma non ho assolutamente idea di cosa sia sbagliato ...

Apprezzerei qualsiasi aiuto!

Grazie in anticipo

È stato utile?

Soluzione

Le miniere funzionano bene, prova le seguenti modifiche.

  1. se stai usando Junit4 non è necessario estendere la tua classe di prova, il runner junit dovrebbe fare il trucco
  2. Carica la configurazione del contesto tramite classpath e assicurati che sia accessibile dal classpath di test

    @ContextConfiguration(locations={"classpath:applicationContext-test.xml"})

  3. quindi prova solo i controller annotati.Lo faccio in questo modo:

    @Test
    @Transactional
    public void testAnnotatedListUser() throws Exception {
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        AnnotationMethodHandlerAdapter handlerAdpt = new AnnotationMethodHandlerAdapter();
        request.setRequestURI("/you/URIhere");
        ModelAndView mav = handlerAdpt.handle(request, response, this.controller);
        assertEquals("Incorrect view name returned", "myexpectedviewname", mav.getViewName());
    }

Altri suggerimenti

Ci sono diversi problemi nella mia domanda:

All'inizio, non è possibile estendere AbstractJUnit4SpringContextTests e usare @RunWith (...), perché è lo stesso.

In secondo luogo, non dovresti usare il dispatcherServlert, ma un Handler definendo il gestore in application.xml e eseguendolo automaticamente nel caso di test tramite @Autowire private Handler handler ...

Allora tutto dovrebbe funzionare bene!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top