java - 实现 Spring Controller 的单元测试时缺少依赖项 hasSize() 和 hasProperty()

标签 java spring unit-testing spring-mvc dependencies

我正尝试在 Spring MVC Controller 中实现一个方法的单元测试,如下所示:

@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;

最佳答案

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

import static org.hamcrest.CoreMatchers.*;

hasPropertyhasSize 方法仅在Matchers 中hamcrest-library 的类,其中包括一组更大的匹配器。尝试将您的导入更改为以下内容:

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>

关于java - 实现 Spring Controller 的单元测试时缺少依赖项 hasSize() 和 hasProperty(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24588056/

相关文章:

java - jOOQ:模拟 DAO 对象

ios - 在单元测试中,验证使用参数 NSData 调用的函数(其中包含 NSString)

java - 无法在 ubuntu 上为 eclipse 设置 lwjgl

java - 在 WebServices 插件设置中设置有效的 CXF 路径

java - 我在哪里可以在线找到包含(大部分)完整的 Java 单元测试套件的开源 Web 应用程序实现?

java - Swagger 与 spring MVC 不生成服务文档

java - 组织.hibernate.HibernateException : 'hibernate.dialect' must be set when no Connection available

java - 将 printf %f %n 与 double 一起使用,然后添加字符串时遇到问题吗?

java - 由于 android studio 和 adb.exe 的更新,我无法运行我的程序

Java 数组声明如 { A[] aa = new B[10]}