【问题标题】:How To Write Mockito test case如何编写 Mockito 测试用例
【发布时间】:2013-07-29 03:56:42
【问题描述】:

我是 spring 和 junit 的新手。我想使用 mockito 测试我的控制器。我使用 mock-mvc 编写了测试用例 但我的一位前辈告诉我试试 mockito。我在 google 中搜索过,我不知道 mockito 单元测试。

@Autowired
private Client client;

 @RequestMapping(value = "/user", method = RequestMethod.GET)
    public String initUserSearchForm(ModelMap modelMap) {
        User user = new User();
        modelMap.addAttribute("User", user);
        return "user";
    }

    @RequestMapping(value = "/byName", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public
    @ResponseBody
    String getUserByName(HttpServletRequest request,@ModelAttribute("userClientObject") UserClient userClient) {
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        return client.getUserByName(userClient, firstName, lastName);
    }

我的 mock-mvc 测试用例是

@Test
    public void testInitUserSearchForm() throws Exception {
        this.liClient = client.createUserClient();
        mockMvc.perform(get("/user"))
                .andExpect(status().isOk())
                .andExpect(view().name("user"))
                .andExpect(forwardedUrl("/WEB-INF/pages/user.jsp"));
    }

    @Test
    public void testGeUserByName() throws Exception {
        String firstName = "Wills";
        String lastName = "Smith";         
        mockMvc.perform(get("/user-byName"))
                .andExpect(status().isOk());

    }

谁能帮帮我?

【问题讨论】:

  • 你能发布你的“getUserByName(userClient, firstName, lastName);”吗?实现代码sn-p?
  • 我从 jar 中调用了 getUserByName 方法。它是一个单独的 API
  • 它是静态方法,因为我没有看到对象引用吗?
  • 不,这不是静态方法。我用的是队友的jar文件。
  • 没看懂,应该是“someObject.getUserByName()”吧?

标签: spring spring-mvc junit4 mockito


【解决方案1】:

1.在xml中定义这个客户端mockito,我们称之为client-mock.xml

<bean id="client" class="org.mockito.Mockito" factory-method="mock">
    <constructor-arg value="your org.Client" /> //interface name
</bean>

如果 Client 不是接口,您可能必须将 cglib 添加到类路径中。

2.将您的客户端“真实”与 your-servlet-context.xml 分开,这样它就不会在测试中加载。

import static org.mockito.Mockito.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:your-servlet-context.xml",
    "classpath:client-mock.xml" })
@WebAppConfiguration
public class YourTests {

    @Autowired
    private Client client;

    @Test
    public void testGeUserByName() throws Exception {
        String firstName = "Wills";
        String lastName = "Smith";         
        String returning = //JSON I presume 

        UserClient userClientObject = ;//init

        when(client).getUserByName(userClientObject, firstName, lastName)
        .thenReturn(returning);//define stub call expectation


        mockMvc.perform(get("/user-byName").sessionAttr("userClientObject", userClientObject))
            .andExpect(status().isOk());

    }

}

顺便说一句,如果在测试中使用“真实”客户端不是很复杂或成本不是很高,那么是否使用 mockito 并不重要。

您可以在此处获取Mockito Doc

【讨论】:

  • org.Client 是一个类文件。当我添加 @ContextConfiguration(locations = {"classpath:META-INF/client-dispatcher-servlet.xml"}) 它在测试用例上显示错误
  • 顶级元素未完成。我正在使用 intellij
  • 如果我添加这个@ContextConfiguration("file:src/main/webapp/WEB-INF/client-dispatcher-servlet.xml") 它工作正常。我无法添加 mock.xml
  • 我的问题是当我的 mock.xml 只有它显示错误。我是否需要在 web.xml 中添加一个条目?
  • 是否可以在 mockito 中测试视图,例如` @Test public void testInitSorm() throws Exception { mockMvc.perform(get("/client/form")) .andExpect(status(). isOk()) .andExpect(view().name("user")) .andExpect(forwardedUrl("/WEB-INF/pages/user.jsp")); }`
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-05
  • 2017-09-06
  • 1970-01-01
  • 2019-06-21
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
相关资源
最近更新 更多