Java 实现 Spring 控制器单元测试时缺少依赖 hasSize() 和 hasProperty()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24588056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Missing dependencies hasSize() and hasProperty() when implementing unit test of Spring controller
提问by Mr Morgan
I am trying to implement a unit test of a method in a Spring MVC controller as follows:
我正在尝试在 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("[email protected]");
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("[email protected]")),
)
)));
}
catch(Exception e) {
Misc.printStackTrace(e);
}
verify(mockPersonService, times(1)).get(1);
verifyNoMoreInteractions(mockPersonService);
}
But I get messages about dependencies concerning hasSize(long)
and hasProperty(...)
.
但是我收到了关于hasSize(long)
和依赖项的消息hasProperty(...)
。
I have recent versions of Mockito, HamCrest et al in the application's class path.
我在应用程序的类路径中有 Mockito、HamCrest 等的最新版本。
So what am I missing?
那么我错过了什么?
My current imports are:
我目前的进口是:
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;
采纳答案by andersschuller
Your imports only include the matchers from hamcrest-core, which are in CoreMatchers
:
您的导入仅包括来自 hamcrest-core 的匹配器,它们位于CoreMatchers
:
import static org.hamcrest.CoreMatchers.*;
The hasProperty
and hasSize
methods are only in the Matchers
class of hamcrest-library, which includes a larger set of matchers. Try to change your import to the following:
的hasProperty
和hasSize
方法仅在Matchers
类hamcrest库,其包括一大组的匹配器构成。尝试将您的导入更改为以下内容:
import static org.hamcrest.Matchers.*;
If this does not work, you might only be depending on hamcrest-core. In this case, change your dependency to the hamcrest-library or hamcrest-all artifact. An example for adding this dependency for Maven is below. See the Hamcrest GitHub pagefor some more details.
如果这不起作用,您可能只依赖 hamcrest-core。在这种情况下,请将您的依赖项更改为 hamcrest-library 或 hamcrest-all 工件。下面是为 Maven 添加此依赖项的示例。有关更多详细信息,请参阅Hamcrest GitHub 页面。
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
</dependency>