Mockito间谍 - 在调用构造函数之前存根 [英] Mockito Spy - stub before calling the constructor

查看:141
本文介绍了Mockito间谍 - 在调用构造函数之前存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图窥探一个Object,我想在构造函数调用它之前存根一个构造函数调用的方法。

我的类看起来像这样:

I'm trying to spy on an Object and I want to stub a method that is called by the constructor before the constructor calls it.
My class looks like that:

public class MyClass {
    public MyClass() {
         setup();
    }

    public void setup() {

    }
}

不得调用设置方法。好吧,我如何监视这个方法(和存根设置,使它什么也不做)?

它可以很好地模拟方法,但我想单元测试 MyClass 所以我需要其他方法。

The setup method mustn't be called. Well, how do I spy on this method (and stub setup so that it does nothing)?
It works fine with mocking the method but I want to unit test MyClass and so I will need very other method.

之所以需要存根设置方法以便它什么都不做:

我'我编写了一个乐高机器人(lejos),并在机器人需要工作的设置中加入了一些代码。但是,当我在TinyVM(安装在机器人上的VM)之外调用它时,java崩溃,因为VM尚未正确初始化(因为测试在我的PC上运行)。对于单元测试,设置并不重要。

我无法对类/方法设置调用进行存根,因为其中一些是公共静态最终变量。

The reason why need to stub the setup method so that it does nothing:
I'm programing a Lego robot (lejos) and I put some code in setup that the robot needs to work. However, when I call it outside TinyVM (the VM that is installed on the robot), java crashes since it the VM hasn't been initialized properly (because the tests run on my PC). For unit-testing the setup isn't important.
I can't stub the classes/methods setup calls since some of them are public static final variables.

推荐答案

感谢您的建议,但有点过于复杂。

我最后嘲笑通过扩展类并覆盖我的设置方法的方法。这样默认构造函数不会调用它的setup实现,它会调用覆盖的方法。

这是代码:

Thanks for the suggestions, but it was a little bit too complex.
I ended up mocking the method by extending the class and overwriting my setup method. This way the default constructor won't call its implementation of setup, it will call the overwritten method instead.
Here is the code:

// src/author/MyClass.java

public class MyClass {
    public MyClass() {
        setup();
    }

    protected void setup() {
        throw new Exception("I hate unit testing !");
    }

    public boolean doesItWork() {
        return true;
    }
}

// test/author/MyClass.java

public class MyClassTest {
    private class MockedMyClass extends MyClass {
        @Override
        protected void setup() {

        }
    }

    private MyClass instance;

    @Before
    public void setUp() { // Not to be confusing with `MyClass#setup()`!
        instance = new MockedMyClass();
    }

    @Test
    public void test_doesItWork() {
        assertTrue(instance.doesItWork());
    }

}

如果你不想要MyTest的话除了你的测试之外,其他子类调用或覆盖的setup方法(因为其他开发人员可能会使用setup方法搞得非常糟糕),只需将可见性更改为default,只有你的类才能调用setup。

If you don't want MyTest's setup method to do called or overwritten by other subclasses except your test (because other developer might mess things up very badly by using the setup method), just change the visibility to default and only your classes will be able to call setup.

如果有更简单的方法,请回答这个问题,因为我不是100%满意我的解决方案。

If there is a simpler way, please answer the question because I'm not 100% content with my solution.

这篇关于Mockito间谍 - 在调用构造函数之前存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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