English 中文(简体)
春季3.0 Servlet
原标题:Spring 3.0 junit test DispatcherServlet

我正试图与陪审团一道测试我的申请。

因此,我设立了以下类别:

@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

}

So the problem is, that it seems that my dispatcher servlet cannot send any request to any of my controllers.

I think that there is something with the configuration - contextConfigurationLocation. It looks like he can find the file (otherwise it would throw an exception) , but doesn t load any configuration

The logger says:

org.childrenframework.web.servlet.Page Not Found - Nomap found for HTTP request with URI [http:// localhost:80/myapp/abc]

但我绝对不认为什么是错的。

我感谢任何帮助!

预 收

最佳回答

矿井工作得很重,试着下列拖.。

  1. if you re using Junit4 no need to extend you test class, the junit runner should do the trick
  2. 通过班次进行背景审查,并确保从测试班获得。

    @ContextConfiguration (positions={”classpath:applicationContext-test.xml”}

  3. 然后对附加说明的控制器进行测试。 我这样做:


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

问题回答

我的问题是:

首先,不可能扩大摘要JUnit4 SpringContext Tests and use @RunWith(......),因为它也是这样。

第二,你不应使用发送机Servlert,而应使用手稿,在你的申请中界定手稿,并在测试案例中通过@Autowire private Handlerhandler.

那么,一切工作都应当细致。





相关问题
array dependency injection in spring?

is there a way to use dependency injection to inject all available implementations of a specific interface in spring? This is kind of the same thing as asked here for .NET. Though my aim is to use @...

Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

How can I determine Objects in application context?

I am trying to write a portlet for Liferay (using Tomcat and Spring) and need to use a database via Persistence API/Hibernate. I am using some configuration XMLs (applicationContext.xml, etc.) and ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

热门标签