具有局部变量的Mockito [英] Mockito with local variables

查看:96
本文介绍了具有局部变量的Mockito的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回String的简单方法.

它还会创建一个本地List.我想测试添加到本地List的值.

这是一个例子

package com.impl;

import java.util.ArrayList;
import java.util.List;

import com.test.domain.CustomerVo;

public class ClassImpl {

    public String assignGift(CustomerVo customerVo) {
        List<String> listOfGift = new ArrayList<String>();

        if (customerVo.getName().equals("Joe")) {
            listOfGift.add("ball");
        } else if ((customerVo.getName().equals("Terry"))) {
            listOfGift.add("car");
        } else if (customerVo.getName().equals("Merry")) {
            listOfGift.add("tv");
        }else {
            listOfGift.add("no gift");
        }

        return "dummyString";
    }
}

在将customerVo.getName.equals("Terry")car添加到本地List时,如何测试.

解决方案

这并不那么容易.

您需要使用 powermock 之类的东西.

使用powermock可以在调用方法并将其回放之前创建一个场景,这意味着您可以告诉ArrayList类构造函数以预期会被调用并返回mock而不是真实的ArrayList. /p>

这将允许您在mock上进行断言.

类似的事情应该起作用:

ArrayList listMock = createMock(ArrayList.class);
expectNew(ArrayList.class).andReturn(listMock);

因此,当您的方法创建本地List时,powermock实际上将返回模拟List.

更多信息此处.

这种模拟实际上是针对未编写为可测试的旧代码的单元测试.如果可能的话,我强烈建议您重写代码,这样就不必进行这种复杂的模拟了.

I have a simple method that returns a String.

It also creates a local List. I want to test the value added to the local List.

Here is an example

package com.impl;

import java.util.ArrayList;
import java.util.List;

import com.test.domain.CustomerVo;

public class ClassImpl {

    public String assignGift(CustomerVo customerVo) {
        List<String> listOfGift = new ArrayList<String>();

        if (customerVo.getName().equals("Joe")) {
            listOfGift.add("ball");
        } else if ((customerVo.getName().equals("Terry"))) {
            listOfGift.add("car");
        } else if (customerVo.getName().equals("Merry")) {
            listOfGift.add("tv");
        }else {
            listOfGift.add("no gift");
        }

        return "dummyString";
    }
}

How can I test that when the customerVo.getName.equals("Terry"), car is added to the local List.

解决方案

This isn't altogether that easy.

You need to use something like powermock.

With powermock you create create a scenario before then method is called and play it back, this means you can tell the ArrayList class constructor to anticipate being called and return a mock rather than a real ArrayList.

This would allow you to assert on the mock.

Something like this ought to work:

ArrayList listMock = createMock(ArrayList.class);
expectNew(ArrayList.class).andReturn(listMock);

So when your method creates the local List powermock will actually return your mock List.

More information here.

This sort of mocking is really for unit testing of legacy code that wasn't written to be testable. I would strongly recommend, if you can, rewriting the code so that such complex mocking doesn't need to happen.

这篇关于具有局部变量的Mockito的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆