【问题标题】:Missing dependencies hasSize() and hasProperty() when implementing unit test of Spring controller在实现 Spring 控制器的单元测试时缺少依赖项 hasSize() 和 hasProperty()
【发布时间】:2014-08-26 14:31:33
【问题描述】:

我正在尝试在 Spring MVC 控制器中实现方法的单元测试,如下所示:

@Test
public void testGetProfile() {
    Person mockPerson = new Person();
    mockPerson.setPersonId(1);
    mockPerson.setName("Mr Brown");
    mockPerson.setAddress("Somewhere");
    mockPerson.setTelephone("1234567890"); 
    mockPerson.setEmail("brown@brown.com");

    when(mockPersonService.get(1)).thenReturn(mockPerson);
    try {
        mockMvc.perform(get("/person/profile?personId=1"))
            .andExpect(status().isOk())
            .andExpect(view().name("view/profile"))
            .andExpect(forwardedUrl("/WEB-INF/jsp/view/profile.jsp"))
            .andExpect(model().attribute("person", hasSize(1L)))
            .andExpect(model().attribute("person", hasItem(
                    allOf(
                            hasProperty("personId", is(1L)),
                            hasProperty("name", is("Mr Brown")),
                            hasProperty("address", is("Somewhere")),
                            hasProperty("telephone", is("1234567890")),
                            hasProperty("email", is("brown@brown.com")),
                    )
            )));

    }
    catch(Exception e) {
        Misc.printStackTrace(e);
    }
    verify(mockPersonService, times(1)).get(1);
    verifyNoMoreInteractions(mockPersonService);        
}

但我收到有关 hasSize(long) 和 hasProperty(...) 的依赖关系的消息。

我在应用程序的类路径中有最新版本的 Mockito、HamCrest 等。

那么我错过了什么?

我目前的进口是:

import library.model.Person;
import library.service.PersonService;
import library.util.Misc;
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.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.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; 
import static org.hamcrest.CoreMatchers.*;
import static org.junit.matchers.JUnitMatchers.hasItem;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;

【问题讨论】:

  • 您收到的完整错误消息是什么?您是否使用静态导入导入了 hasSize 和 hasProperty?
  • 我正在使用 NetBeans 并收到提示“找不到符号方法 hasProperty(String, Matcher)”的消息。这些消息不在堆栈跟踪或编译器列表中。
  • 听起来您可能没有导入该方法。您能否在问题中包含来自您的测试类的导入?
  • 我已将它们添加到问题中。

标签: java spring unit-testing spring-mvc dependencies


【解决方案1】:

您的导入仅包括来自 hamcrest-core 的匹配器,它们位于 CoreMatchers:

import static org.hamcrest.CoreMatchers.*;

hasProperty 和 hasSize 方法仅在 hamcrest-library 的 Matchers 类中,其中包含更大的匹配器集。尝试将您的导入更改为以下内容:

import static org.hamcrest.Matchers.*;

如果这不起作用,您可能只依赖于 hamcrest-core。在这种情况下,将您的依赖项更改为 hamcrest-library 或 hamcrest-all 工件。下面是为 Maven 添加此依赖项的示例。有关更多详细信息,请参阅Hamcrest GitHub page。

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-library</artifactId>
    <version>1.3</version>
</dependency>

【讨论】:

猜你喜欢
  • 2012-07-16
  • 1970-01-01
  • 1970-01-01
  • 2011-06-03
  • 1970-01-01
  • 2021-10-26
  • 1970-01-01
  • 2020-01-05
  • 1970-01-01
相关资源
最近更新 更多