使用Mockitos传递参数化输入

spa*_*pal 18 java unit-testing mockito

我正在使用Mockito进行单元测试.我想知道是否可以像在Junit测试中一样发送参数化输入参数,
例如

@InjectMocks
MockClass mockClass = new MockClass();

@Test
public void mockTestMethod()
    {
    mockClass.testMethod(stringInput); 
// here I want to pass a list of String inputs 
// this is possible in Junit through Parameterized.class.. 
// wondering if its can be done in Mockito
    } 
Run Code Online (Sandbox Code Playgroud)

Jef*_*ica 22

在JUnit中,参数化测试使用特殊的运行器来确保测试多次实例化,因此每次测试方法都会被多次调用.Mockito是用于编写特定单元测试的工具,因此没有内置功能可以多次运行相同的测试,具有不同的Mockito期望.

如果您希望更改测试条件,最好的办法是执行以下操作之一:

  • 使用JUnit参数化您的测试,并使用您想要的模拟输入的参数;
  • 在测试中运行不同参数的循环,遗憾的是避免了"每种方法测试一件事"的理念
  • 提取实际执行测试的方法,并@Test为您想要的每个模拟创建一个新方法.

请注意,没有禁止使用模拟对象作为@Parameterized测试参数.如果您正在寻找基于模拟的参数化,您可以这样做,可能创建模拟并在测试中以静态方法设置期望.


关于跑步者的注意事项:此参数化测试跑者与Mockito的MockitoJUnitRunner冲突:每个测试类只能有一个跑步者.如果你同时使用它们,你会想要切换到@Before和@After方法Mockito JUnit4规则进行设置.

例如,从不同的答案压缩,解释更多关于参数化运行器与JUnit规则的关系,并从JUnit4参数化测试文档页面和MockitoRule doc页面解除:

@RunWith(Parameterized.class)
public class YourComponentTest {
    @Rule public MockitoRule rule = MockitoJUnit.rule();
    @Mock YourDep mockYourDep;

    @Parameters public static Collection<Object[]> data() { /* Return the values */ }

    public YourComponentTest(Parameter parameter) { /* Save the parameter to a field */ }

    @Test public void test() { /* Use the field value in assertions */ }
}
Run Code Online (Sandbox Code Playgroud)


kav*_*i77 6

如果您不熟悉较旧版本的mockito,MockitoRule则另一种可能性是使用以下方法明确初始化模拟MockitoAnnotations.initMocks:

@RunWith(Parameterized.class)
public class YourComponentTest {        
    @Mock YourDep mockYourDep;

    @Parameter
    public Parameter parameter;

    @Parameters public static Collection<Object[]> data() { /* Return the values */ }

    @Before
    public void init() {
        MockitoAnnotations.initMocks(this);
    }

    @Test public void test() { /* Use the field value in assertions */ }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

您可以使用JUnitParamsRunner。这是我的方法:

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import java.util.Arrays;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

@RunWith(value = JUnitParamsRunner.class)
public class ParameterizedMockitoTest {

    @InjectMocks
    private SomeService someService;
    @Mock
    private SomeOtherService someOtherService;

    @Before
    public void setup() {
        initMocks(this);
    }

    @Test
    @Parameters(method = "getParameters")
    public void testWithParameters(Boolean parameter, Boolean expected) throws Exception {
        when(someOtherService.getSomething()).thenReturn(new Something());
        Boolean testObject = someService.getTestObject(parameter);
        assertThat(testObject, is(expected));
    }

    @Test
    public void testSomeBasicStuffWithoutParameters() {
        int i = 0;
        assertThat(i, is(0));
    }

    public Iterable getParameters() {
        return Arrays.asList(new Object[][]{
                {Boolean.TRUE, Boolean.TRUE},
                {Boolean.FALSE, Boolean.FALSE},
        });
    }
}
Run Code Online (Sandbox Code Playgroud)