在实现Spring控制器的单元测试时,缺少依赖项hasSize()和hasProperty()

Mr *_*gan 9 java spring dependencies unit-testing spring-mvc

我试图在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);        
}
Run Code Online (Sandbox Code Playgroud)

但我得到有关消息的依赖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;
Run Code Online (Sandbox Code Playgroud)

and*_*ler 20

您的进口仅包括hamcrest-core的匹配器,它们位于CoreMatchers:

import static org.hamcrest.CoreMatchers.*;
Run Code Online (Sandbox Code Playgroud)

hasPropertyhasSize方法仅在Matchers类hamcrest库,其包括一大组的匹配器构成.尝试将导入更改为以下内容:

import static org.hamcrest.Matchers.*;
Run Code Online (Sandbox Code Playgroud)

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

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-library</artifactId>
    <version>1.3</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)