当前位置: 首页 > 知识库问答 >
问题:

想要使用@mock和@injectmock的Mockito,但没有调用:实际上,与这个mock没有任何交互

巫马越彬
2023-03-14
   public class DivisionCalculator {
    private final Validator validator;
    private final MathProvider mathProvider;
    private final ViewProvider viewProvider;

    public DivisionCalculator(Validator validator, MathProvider mathProvider, ViewProvider viewProvider) {
        this.validator = validator;
        this.mathProvider = mathProvider;
        this.viewProvider = viewProvider;
    }

    public String calculate(int dividend, int divisor) {
        validator.validate(dividend, divisor);

        List<DivisionStep> items = mathProvider.calculate(dividend, divisor);

        DivisionResult result = DivisionResult.builder()
                .dividend(dividend)
                .divisor(divisor)
                .finalResult(mathProvider.finalResult(items))
                .items(items)
                .build();

        return viewProvider.provideView(result);
    }
}
    public class DivisionCalculatorTest {

    private ViewProvider viewProvider = Mockito.mock(ViewProvider.class);
    private MathProvider mathProvider = Mockito.mock(MathProvider.class);
    private Validator validator = Mockito.mock(Validator.class);

    private DivisionCalculator divisionCalculator = new DivisionCalculator(validator, mathProvider, viewProvider);

    DivisionResult makeDivision(int dividend, int divisor) {
        List<DivisionStep> items = mathProvider.calculate(dividend, divisor);
        return DivisionResult.builder()
                .dividend(dividend)
                .divisor(divisor)
                .finalResult(mathProvider.finalResult(items))
                .items(items)
                .build();
    }

    @Test
    void divisionCalculatorShouldSuccessfullyCallHisComponents() {

        divisionCalculator.calculate(4, 2);

        Mockito.verify(validator).validate(Mockito.anyInt(), Mockito.anyInt());
        Mockito.verify(mathProvider).calculate(Mockito.anyInt(),Mockito.anyInt());
        Mockito.verify(viewProvider).provideView(refEq(makeDivision(4, 2)));

    }
}
public class DivisionTest {
    @Mock
    private Validator validator = new Validator();
    @Mock
    private ViewProvider viewProvider = new ViewProvider();
    @Mock
    private MathProvider mathProvider = new MathProvider();

    @BeforeEach
    void setup() {
        MockitoAnnotations.initMocks(this);
    }
    @InjectMocks
    private DivisionCalculator divisionCalculator = new DivisionCalculator(validator, mathProvider, viewProvider);

    DivisionResult makeDivision(int dividend, int divisor) {
        List<DivisionStep> items = mathProvider.calculate(dividend, divisor);
        return DivisionResult.builder()
                .dividend(dividend)
                .divisor(divisor)
                .finalResult(mathProvider.finalResult(items))
                .items(items)
                .build();
    }

    @Test
    void divisionCalculatorShouldSuccessfullyCallHisComponents() {

        divisionCalculator.calculate(4, 2);

        Mockito.verify(validator).validate(4, 2);
        Mockito.verify(mathProvider).calculate(4, 2);
        Mockito.verify(viewProvider).provideView(refEq(makeDivision(4, 2)));
    }
}

在执行以下步骤后,我得到了错误:想要但没有调用:实际上,与这个模拟没有任何交互。我不明白我在这里遗漏了什么。

共有1个答案

燕星鹏
2023-03-14

如果有像@mock@injectmocks这样的注释,则需要在JUnit5中使用一个特殊的扩展来运行Junit测试:

@ExtendWith(MockitoExtension.class)
class MyTest {
    @Mock
    private Validator validator;
     ...
}

还要注意,这个扩展为您创建模拟,并将值注入到带注释的数据字段中。因此,从技术上来说,创建像问题中所示的真实对象是错误的:

public class DivisionTest {
    @Mock
    private Validator validator = new Validator();  // this is wrong, don't use "new"
    @Mock
    private ViewProvider viewProvider = new ViewProvider(); // this is wrong, don't use "new"
    @Mock
    private MathProvider mathProvider = new MathProvider(); // this is wrong, don't use "new"

您可以按照本教程进行更深入的示例和解释。

@RunWith(MockitoJUnitRunner.class)
public class DivisionTest {...}
 类似资料: