Java MockMvc:forwardedUrl为空

Java MockMvc:forwardedUrl为空,java,spring,mocking,mockito,spring-mvc-test,Java,Spring,Mocking,Mockito,Spring Mvc Test,我在做这个测试时遇到了问题。断言总是失败,因为它无法验证始终为空的forwardedUrl。如果我去掉这个假设,它就无法验证回报模型。我想这是因为缺少转发的Url。我的测试类如下所示: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:test-application-config.xml"}) @WebAppConfiguration public class UserCo

我在做这个测试时遇到了问题。断言总是失败,因为它无法验证始终为空的forwardedUrl。如果我去掉这个假设,它就无法验证回报模型。我想这是因为缺少转发的Url。我的测试类如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:test-application-config.xml"})
@WebAppConfiguration
public class UserControllerTest {
  private MockMvc mockMvc;

  @Mock
  private UserService userServiceMock;

  @Inject 
  private WebApplicationContext wac;

  @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);
    mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
  }

  @Test
  public void users_ShouldAddUserEntriesToModelAndRenderUserListView() throws Exception {
    User firstUser = generateSampleUser("Pera", "Peric");
    User secondUser = generateSampleUser("Maja", "Majic");

    when(userServiceMock.getAllUsers()).thenReturn(Arrays.asList(firstUser, secondUser));

    mockMvc.perform(get("/users"))
         .andExpect(status().isOk())
         .andExpect(view().name("users"))
         .andExpect(forwardedUrl("/WEB-INF/velocity/users.vm"))
         .andExpect(model().attribute("users", hasSize(2)))
         .andExpect(model().attribute("users", hasItem(
                 allOf(
                         hasProperty("id", is(1L)),
                         hasProperty("firstName", is("Pera")),
                         hasProperty("LastName", is("Peric"))
                 )
         )))
         .andExpect(model().attribute("users", hasItem(
                 allOf(
                         hasProperty("id", is(2L)),
                         hasProperty("firstName", is("Maja")),
                         hasProperty("LastName", is("Majic"))
                 )
         )));

    verify(userServiceMock, times(1)).getAllUsers();
    verifyNoMoreInteractions(userServiceMock);
  }

  private User generateSampleUser(String firstName, String lastName) {
    User user = new User();
    user.setFirstName(firstName);
    user.setLastName(lastName);
    user.setEmail("test@email.com");
    user.setBirthday(new Date());
    user.setGender(Gender.MALE);
    user.setPersonalNumber("1234567890123");

    return user;
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<import resource="classpath:data-config-hsql.xml" />

<mvc:annotation-driven />

<context:component-scan base-package="com.code9" />

<bean id="velocityConfig"
    class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
    <property name="resourceLoaderPath" value="/WEB-INF/velocity/" />
</bean>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
    <property name="cache" value="true" />
    <property name="prefix" value="" />
    <property name="layoutUrl" value="layout.vm" />
    <property name="suffix" value=".vm" />
    <property name="exposeSessionAttributes" value="true" />
</bean>
测试配置文件如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:test-application-config.xml"})
@WebAppConfiguration
public class UserControllerTest {
  private MockMvc mockMvc;

  @Mock
  private UserService userServiceMock;

  @Inject 
  private WebApplicationContext wac;

  @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);
    mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
  }

  @Test
  public void users_ShouldAddUserEntriesToModelAndRenderUserListView() throws Exception {
    User firstUser = generateSampleUser("Pera", "Peric");
    User secondUser = generateSampleUser("Maja", "Majic");

    when(userServiceMock.getAllUsers()).thenReturn(Arrays.asList(firstUser, secondUser));

    mockMvc.perform(get("/users"))
         .andExpect(status().isOk())
         .andExpect(view().name("users"))
         .andExpect(forwardedUrl("/WEB-INF/velocity/users.vm"))
         .andExpect(model().attribute("users", hasSize(2)))
         .andExpect(model().attribute("users", hasItem(
                 allOf(
                         hasProperty("id", is(1L)),
                         hasProperty("firstName", is("Pera")),
                         hasProperty("LastName", is("Peric"))
                 )
         )))
         .andExpect(model().attribute("users", hasItem(
                 allOf(
                         hasProperty("id", is(2L)),
                         hasProperty("firstName", is("Maja")),
                         hasProperty("LastName", is("Majic"))
                 )
         )));

    verify(userServiceMock, times(1)).getAllUsers();
    verifyNoMoreInteractions(userServiceMock);
  }

  private User generateSampleUser(String firstName, String lastName) {
    User user = new User();
    user.setFirstName(firstName);
    user.setLastName(lastName);
    user.setEmail("test@email.com");
    user.setBirthday(new Date());
    user.setGender(Gender.MALE);
    user.setPersonalNumber("1234567890123");

    return user;
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<import resource="classpath:data-config-hsql.xml" />

<mvc:annotation-driven />

<context:component-scan base-package="com.code9" />

<bean id="velocityConfig"
    class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
    <property name="resourceLoaderPath" value="/WEB-INF/velocity/" />
</bean>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
    <property name="cache" value="true" />
    <property name="prefix" value="" />
    <property name="layoutUrl" value="layout.vm" />
    <property name="suffix" value=".vm" />
    <property name="exposeSessionAttributes" value="true" />
</bean>

如果使用生产配置文件而不是
测试应用程序配置
,它是否有效

e、 g

也许您需要在
resourceLoaderPath
中添加一些
。/

e、 g



Ok。这里有一个问题:java.io.FileNotFoundException:类路径资源[Baddington/src/main/webapp/WEB-INF/velocity/]无法解析为URL,因为它不存在。我的问题是如何引用其他源文件夹中的路径?请发布完整的堆栈跟踪。我已经添加了完整的堆栈跟踪。您使用Eclipse吗?您的
src/main/webapp
文件夹是源文件夹吗?是的,我使用Eclipse。不,不是。
@ContextConfiguration({"file:src/main/webapp/WEB-INF/applicationContext.xml", 
     "file:src/main/webapp/WEB-INF/dispatcher-servlet.xml"})
<property name="resourceLoaderPath" value="../main/webapp/WEB-INF/velocity/" />