繁体   English   中英

JMockit - 如何注入抽象方法

[英]JMockit - How to inject an Abstract method

我正在尝试为扩展抽象类的类编写单元测试,但测试仍在尝试调用真正的抽象方法。 有没有办法注入 Mocked 抽象类并验证是否调用了抽象方法?

测试

public class TestThisClassTest {
    @Tested
    TestThisClass testThisClass;

    @Injectable
    String names;
    @Injectable
    String username;
    @Injectable
    char[] password = {'t', 'e', 's', 't', 's'};;
    @Injectable
    String destinationName;

    @Injectable
    AbstractClass abstractClass; // Thought this would inject but it's not

    @Test(description = "Verify that sendMessageAbstractMethod is called")
    public void testSendMessage(@Mocked ObjectMessage message) throws Exception {

        testThisClass.sendMessage(message); // This is instantiating AbstractClass when it shouldn't be
        new Verifications(){{
            abstractClass.sendMessageAbstractMethod((Object) any);
            times = 1;
        }};
    }
}

TestThisClass.class

public class TestThisClass extends AbstractClass {

    public TestThisClass() {
        super();
    }

    @Inject
    public TestThisClass(String names, String username, char[] password, String destinationName) {
        super(names, username, password, destinationName);
    }

    public void sendMessage(Object message) throws Exception {  // Trying to test this method
        sendMessageAbstractMethod(message);  // This is "doing stuff." Need it verify this is called and move on
     
    }
}

抽象类

public abstract class AbstractClass {
    public AbstractClass(String names, String username, char[] password, String destinationName) {
        this.names = names;
        this.username = username;
        this.password = password;
        this.destinationName = destinationName;
    }


    protected void sendMessageAbstractMethod(Object message) throws Exception {
        //do stuff
    }
}

使用间谍解决了这个问题

public class TestThisClassTest {

    @Injectable
    String names;
    @Injectable
    String username;
    @Injectable
    char[] password = {'t', 'e', 's', 't', 's'};;
    @Injectable
    String destinationName;

    @Test
    public void testSendMessage(@Mocked ObjectMessage message) throws Exception {
        TestThisClass abstractImpl = spy(new TestThisClass());
        doNothing().when(abstractImpl).sendMessageAbstractMethod(any());

        abstractImpl.sendMessage(message));
        new Verifications(){{
            verify(abstractImpl, times(1)).sendMessage(any());
        }};
    }
}

这应该有效:

public class TestThisClassTest {
@Tested
TestThisClass testThisClass;

@Injectable
String names;
@Injectable
String username;
@Injectable
char[] password = {'t', 'e', 's', 't', 's'};;
@Injectable
String destinationName;

@Test(description = "Verify that sendMessageAbstractMethod is called")
public void testSendMessage(@Mocked ObjectMessage message) throws Exception {

    testThisClass.sendMessage(message); 

    new Verifications(testThisClass){{
        testThisClass.sendMessageAbstractMethod((Object) any);
        times = 1;
    }};
}

}

暂无
暂无

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

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