本文介绍了jMockit - 如何进行构造函数调用以返回模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前使用 Mockito + PowerMock 作为我们的主要模拟框架,一旦开始将我们的一些代码移至 Java 8,就遇到了一些问题.因此,我们决定评估 jMockit 作为替代方案.我对 mocking 的概念有很好的理解,但我承认我对 jMockit 的经验非常有限.

We are currently using Mockito + PowerMock as our main mocking framework and had a few issues with this once started moving some of our code to java 8.Because of this we decided to evaluate jMockit as an alternative. I have quite a good understanding of mocking concepts but I admit that my experience with jMockit is very limited.

但是,我在测试一些在我看来应该非常基本的东西时遇到了问题:被测类使用 new 在其构造函数中创建了某个其他类的实例.我想让这个新调用返回一个模拟实例.

However I am having problems with testing something that in my view should be very basic: the class under test creates an instance of some other class inside its constructor using new. That new invocation is the thing that I would like to make return a mock instance.

这是我正在使用的代码:包 com.some.org.some.app;

Here is the code like the one I am using: package com.some.org.some.app;

import mockit.Expectations;
import mockit.Injectable;
import mockit.Mocked;
import org.testng.annotations.Test;

public class ClassUnderTestTest {
    interface SomeService {
        void doWork();
    }

    class ClassUnderTest {
        private final SomeService service;

        public ClassUnderTest() {
            this.service = new SomeService() {
                @Override
                public void doWork() {
                }
            };
        }
    }

    @Injectable
    private SomeService mockService;

    @Test
    public void shouldBeAbleToMaskVariousNumbers() throws Exception {
        new Expectations() {{
            new ClassUnderTest();
            result = mockService;
        }};
    }
}

运行上述程序时,出现以下异常:

When running the above I am getting the exception below:

java.lang.IllegalStateException: Missing invocation to mocked type at this point;
please make sure such invocations appear only after the declaration of a suitable
mock field or parameter

我使用 TestNG 作为测试框架,实际上我的测试方法有一堆参数,因为它期望数据提供者传递一些测试数据.

I am using TestNG as a testing framework and in reality my test method has a bunch of parameters as it is expecting some test data being passed by a data provider.

这是非常基本的,看起来我的方法不是 jMockit 的方法.预先感谢您的支持.

This is pretty basic and it looks like my approach is not the jMockit way. Thank you in advance for your support.

推荐答案

使用 JMockit 很容易做到:

This is very easy to do with JMockit:

public class ClassUnderTestTest {
    interface SomeService { int doWork(); }

    class ClassUnderTest {
        private final SomeService service;

        ClassUnderTest() {
            service = new SomeService() {
                @Override public int doWork() { return -1; }
            };
        }

        int useTheService() { return service.doWork(); }
    }

    // This annotation is a variation on @Mocked, which extends
    // mocking to any implementation classes or subclasses of
    // the mocked base type.
    @Capturing SomeService mockService;

    @Test
    public void shouldBeAbleToMaskVariousNumbers() {
        new Expectations() {{ mockService.doWork(); result = 123; }};

        int n = new ClassUnderTest().useTheService();

        assertEquals(123, n);
    }
}

这篇关于jMockit - 如何进行构造函数调用以返回模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 01:30