使用Mockito存根并执行测试方法 [英] Using Mockito to stub and execute methods for testing

查看:87
本文介绍了使用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.

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

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设置存根(希望是正确的术语),如下所示:

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混淆.

在模拟中,对所有方法进行存根并返回智能返回类型".这意味着,除非指定行为,否则在模拟类上调用任何方法将不执行任何操作.

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.

在间谍中,该类的原始功能仍然存在,但是您可以在间谍中验证方法调用,也可以覆盖方法行为.

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存根并执行测试方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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