使用 Mockito 存根并执行测试方法

时间:2022-12-27
本文介绍了使用 Mockito 存根并执行测试方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近问了几个面向 jUnit 和 Mockito 的问题,但我仍然很难掌握它的窍门.这些教程都是针对非常简单的示例,所以我正在努力扩大我的测试用例以适用于我的课程.

I've asked a couple of jUnit and Mockito oriented questions recently and I'm still really struggling to get the hang of it. The tutorials are all for very simple examples, so I'm struggling to scale up my test cases to work for my classes.

我目前正在尝试为我在 web 应用程序中的一个代理中使用的方法编写一些测试用例.该方法与代理内部的其他几个方法交互以验证某些对象.我现在只想测试这个方法.

I'm currently trying to write some test cases for a method I have in one of my agents in a webapp. The method interacts with a couple of other methods inside the agent to validate some objects. I just want to test this one method right now.

这是我尝试做的:

  1. 像这样创建我的代理的 Mockito 对象:

  1. Create a Mockito object of my agent like so:

MyProcessingAgent mockMyAgent = Mockito.mock(MyProcessingAgent.class);

使用 Mockito.when 设置存根(希望是正确的术语),如下所示:

Setup stubs(hopefully the right term) using Mockito.when like so:

Mockito.when(mockMyAgent.otherMethod(Mockito.any(arg1)).thenReturn(requiredReturnArg);

尝试像这样执行我的方法:

Try executing my method like so:

List myReturnValue = mockMyAgent.methodThatNeedsTestCase();

我期待 myReturnValue 中的内容,但收到 0,所以我尝试调试.当我调用该方法时,它永远不会执行.我在方法的第一行有一个永远不会被触及的调试点.

I was expecting to things in myReturnValue, but received 0 instead so I tried to debug. When I call the method, it never executes. I have a debug point at the first line in the method that never gets touched.

如果我想在类的一个方法中执行代码,但强制类中的其他方法(尝试与外部世界中的数据库交互的方法)返回伪造的值.Mockito 可以做到这一点吗?

If I want to execute the code in one method of a class, but force other methods in the class (one's that try to interact with databases in the outside world) to return faked out values. Is this possible with Mockito?

看来我目前的方法不是正确的测试风格,但我不确定如何继续前进.我可以模拟我的类并让一个方法像平常一样执行,而其他方法被存根以返回我的给定值,这样我就不必在测试这个方法期间处理数据访问?

It appears that my current method of approach is not a correct testing style, but I'm not sure how to move forward. Can I mock my class and have one method be executed like normal while other methods are stubbed to return my given values so that I don't have to deal with data access during testing this one method?

推荐答案

您将 MockSpy 混淆了.

You are confusing a Mock with a Spy.

在模拟中,所有方法都被存根并返回智能返回类型".这意味着调用模拟类的任何方法什么都不做,除非你指定行为.

In a mock all methods are stubbed and return "smart return types". This means that calling any method on a mocked class will do nothing unless you specify behaviour.

在 spy 中,类的原始功能仍然存在,但您可以在 spy 中验证方法调用并覆盖方法行为.

In a spy the original functionality of the class is still there but you can validate method invocations in a spy and also override method behaviour.

你想要的是

MyProcessingAgent mockMyAgent = Mockito.spy(MyProcessingAgent.class);

一个简单的例子:

static class TestClass {

    public String getThing() {
        return "Thing";
    }

    public String getOtherThing() {
        return getThing();
    }
}

public static void main(String[] args) {
    final TestClass testClass = Mockito.spy(new TestClass());
    Mockito.when(testClass.getThing()).thenReturn("Some Other thing");
    System.out.println(testClass.getOtherThing());
}

输出是:

Some Other thing

注意:您真的应该尝试模拟正在测试的类的依赖项而不是类本身.

NB: You should really try to mock the dependencies for the class being tested not the class itself.

这篇关于使用 Mockito 存根并执行测试方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:mockito 回调和获取参数值 下一篇:静态方法是 DI 反模式吗?

相关文章

最新文章