Spring测试在DispatcherServlet中找不到带有URI [/ accueil]的HTTP请求的映射,名称为''

时间:2017-11-22 11:17:28

标签: java spring integration-testing

您好我试图测试我的一个班级,但是当我mvn testNo mapping found for HTTP request with URI [/accueil] in DispatcherServlet with name ''时,我会收到此警告。 这很奇怪,因为我可以在测试之外访问此页面。

这是我要测试的类(我可以通过localhost访问它:8080 / accueil):



package uVote.controllers;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import uVote.DAO.ScrutinDAO;
import uVote.model.Scrutin;
import uVote.model.Votant;

@Controller
@RequestMapping(value = "/accueil")
public class Accueil
{
	@RequestMapping(method = RequestMethod.GET)
	public String accueil(Model model,HttpServletRequest request)
	{		
	    Votant co = VerifCo.isConnecte(request);
	    if(co == null)
		    return "redirect:connexion";
	    else
	    {
		    request.setAttribute("votant", co);
		    List<Scrutin> scrutins = new ScrutinDAO().getToutScrutins();
		    List<Scrutin> scrutinsToShow = new ArrayList<Scrutin>();
		    for(Scrutin s : scrutins)
		    {
			    if(!s.estDansRegistre(co) || s.isTermine())
				    scrutinsToShow.add(s);
		    }
		    request.setAttribute("scrutins", scrutinsToShow);
	    }
	    return "accueil";
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String deconnection(Model model,HttpServletRequest request)
	{
		VerifCo.deconnecteVotant(request);
		return "redirect:accueil";
	}

}
&#13;
&#13;
&#13;

以下是测试用例:

&#13;
&#13;
package uVote.controllers;

import java.util.HashMap;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import uVote.model.Votant;


@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = uVote.controllers.Accueil.class)
/**
 *
 * @author kirua
 */
public class AccueilTest
{
    
    @Autowired
    private WebApplicationContext wac;
    private MockMvc mockMvc;

    
    @Before
    public void setup() 
    {
        DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.wac);
        this.mockMvc = builder.build();
	
	InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/vues/");
        viewResolver.setSuffix(".jsp");
 
        this.mockMvc = MockMvcBuilders.standaloneSetup(new HelpController())
                                 .setViewResolvers(viewResolver)
                                 .build();
    }
    
    @Test
    public void testVotant() throws Exception 
    {
	HashMap<String, Object> sessionattr = new HashMap<String, Object>();
	Votant vo = new Votant(1, "toto_prenom", "toto_nom", "toto_adresse", "mdp");
	sessionattr.put("votant", vo);
       
	ResultMatcher ok = MockMvcResultMatchers.status().isOk();
//	ResultMatcher msg = MockMvcResultMatchers.model()
  //                         .attribute("msg", "Spring quick start!!");

	MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/accueil").sessionAttrs(sessionattr);
	this.mockMvc.perform(builder)
		    .andExpect(ok)
		    .andExpect(view().name("accueil"));
//                   .andExpect(msg);
    }
}
&#13;
&#13;
&#13;

和HelpController:

&#13;
&#13;
public class HelpController extends AbstractController
{

    private static Log log = LogFactory.getLog(HelpController.class);
    
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception
    {
	log.debug("Entering HelpController.handleRenderRequestInternal()");
        ModelAndView modelAndView = new ModelAndView("Help");
        log.debug("Exiting HelpController.handleRenderRequestInternal() " + modelAndView);
        return modelAndView;
    }
    
}
&#13;
&#13;
&#13;

我的web.xml:

&#13;
&#13;
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/root-context.xml</param-value>
	</context-param>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
		
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
</web-app>
&#13;
&#13;
&#13;

我的servlet-context.xml:

&#13;
&#13;
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	
	<!-- Anable du model @Controller -->
	<annotation-driven />

	<!-- Gère les requète d'éléments de ressource (css / js / images ...) -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Résolution des vues utilisées (renvoyées) par les controller - Rendu/Intepretation JSP -->
	<beans:bean id="viewReslover" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--                <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> -->
		<beans:property name="prefix" value="/vues/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>

	
	<context:component-scan base-package="uVote.controllers" />
	
</beans:beans>
&#13;
&#13;
&#13;

和我的root-context:

&#13;
&#13;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
		
</beans>
&#13;
&#13;
&#13;

你知道发生了什么吗?我是java中的菜鸟,所以我不确定问题出在哪里。 请询问您是否需要查看其他文件。

0 个答案:

没有答案