简体   繁体   English

如何使用 JMockit 中的给定构造函数对模拟类的特定实例进行验证?

[英]How to make verifications on specific instances of mocked classes with a given constructor in JMockit?

I've been using JMockit to mock dependencies for a robot (such as sensors, joysticks, etc).我一直在使用 JMockit 来模拟机器人的依赖项(例如传感器、操纵杆等)。 For one of my classes' tests, I need to mock two joystick objects.对于我的课程之一的测试,我需要模拟两个操纵杆对象。

In the code I have, the Joysticks are instantiated as so:在我的代码中,操纵杆被实例化为:

Joystick joystick1 = new Joystick(0);
Joystick joystick2 = new Joystick(1);

I've been reading up on how to match specific instances of objects in theInstance matching section in the JMockit documentation .我一直在阅读有关如何匹配JMockit 文档实例匹配部分中的特定对象实例的信息

These steps however don't seem to work for verifications (or maybe I'm doing it wrong) or they involve using the @Mocked tag in the parameters of every test method which would be a hassle given the fact that I want this behaviour to carry for an entire class of several tests.然而,这些步骤似乎不适用于验证(或者我可能做错了),或者它们涉及在每个测试方法的参数中使用@Mocked标签,鉴于我希望这种行为进行一整类的几个测试。

I cannot simply inject the mocked instances into the tested class since the tested class actually receives them from another class in the application, and I'd rather not mock my own codebase in order to pass in the injectable joysticks.我不能简单地将模拟实例注入到测试类中,因为测试类实际上是从应用程序中的另一个类接收它们的,而且我宁愿不模拟我自己的代码库以传递可注入的操纵杆。

The behaviour that I'm aiming for is something like this:我的目标行为是这样的:

public class ClassUsingJoysticksTest {
    @Injectable
    private Joystick joystick1;

    @Injectable
    private Joystick joystick2;

    @Before
    public void setUp() {
        new Expectations() {{
            new Joystick(0);
            result = joystick1;

            new Joystick(1);
            result = joystick2;
        }};
    }

    @Test
    public void someTest() {
        ClassThatUsesJoysticks classThatUsesJoysticks = new ClassThatUsesJoysticks();
        classThatUsesJoysticks.doSomething();

        new Verifications() {{
            joystick1.setRumble(1D);
            joystick2.setRumble(2D);
        }};
    }
}

Relevant portion of the class under test:被测类的相关部分:

public class ClassThatUsesJoysticks {
    public void doSomething() {
        Joystick joystick1 = OI.getInstance().getJoystick1(); // OI is our own class.
        Joystick joystick2 = OI.getInstance().getJoystick2();

        joystick1.setRumble(1);
        joystick2.setRumble(2);
    }
}

// OI class that we used to keep track of operator interface.
public class OI {
    private Joystick joystick1, joystick2;

    public OI() {
        joystick1 = new Joystick(0); // First joystick is always on port 0.
        joystick2 = new Joystick(1); // Next joystick is port 1.
    }

    public Joystick getJoystick1() {
        return joystick1;
    }

    public Joystick getJoystick2() {
        return joystick2;
    }
}

Is something like this possible using JMockit?使用 JMockit 可以实现这样的事情吗?

If you like, the actual implementation that I'm trying to achieve (with the bad mocking of my own codebase) can be found here .如果您愿意,可以在此处找到我尝试实现的实际实现(对我自己的代码库进行了糟糕的模拟)。

I have this working with the current setup, as documented .文档所示,我使用当前设置进行了此操作。

public class ClassUsingJoysticksTest {
    @Mocked Joystick joystick1;
    @Mocked Joystick joystick2;

    @Before
    public void setUp() {
        new Expectations() {
            new Joystick(0);
            result = joystick1;

            new Joystick(1);
            result = joystick2;
        }
    }

    @Test
    public void testRumbles() {
        classThatUsesJoysticks.doSomething();

        new Verifications() {{
             joystick1.setRumble(1.0);
             joystick2.setRumble(1.0);
        }};
    }
}

If you would like to see the actual implementation, code is here .如果您想查看实际实现,代码在此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM