jmockit-如何注入抽象方法

rqenqsqc  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(356)

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

  1. public class TestThisClassTest {
  2. @Tested
  3. TestThisClass testThisClass;
  4. @Injectable
  5. String names;
  6. @Injectable
  7. String username;
  8. @Injectable
  9. char[] password = {'t', 'e', 's', 't', 's'};;
  10. @Injectable
  11. String destinationName;
  12. @Injectable
  13. AbstractClass abstractClass; // Thought this would inject but it's not
  14. @Test(description = "Verify that sendMessageAbstractMethod is called")
  15. public void testSendMessage(@Mocked ObjectMessage message) throws Exception {
  16. testThisClass.sendMessage(message); // This is instantiating AbstractClass when it shouldn't be
  17. new Verifications(){{
  18. abstractClass.sendMessageAbstractMethod((Object) any);
  19. times = 1;
  20. }};
  21. }
  22. }

testthisclass.class类

  1. public class TestThisClass extends AbstractClass {
  2. public TestThisClass() {
  3. super();
  4. }
  5. @Inject
  6. public TestThisClass(String names, String username, char[] password, String destinationName) {
  7. super(names, username, password, destinationName);
  8. }
  9. public void sendMessage(Object message) throws Exception { // Trying to test this method
  10. sendMessageAbstractMethod(message); // This is "doing stuff." Need it verify this is called and move on
  11. }
  12. }

抽象类

  1. public abstract class AbstractClass {
  2. public AbstractClass(String names, String username, char[] password, String destinationName) {
  3. this.names = names;
  4. this.username = username;
  5. this.password = password;
  6. this.destinationName = destinationName;
  7. }
  8. protected void sendMessageAbstractMethod(Object message) throws Exception {
  9. //do stuff
  10. }
  11. }
disbfnqx

disbfnqx1#

这应该起作用:

  1. public class TestThisClassTest {
  2. @Tested
  3. TestThisClass testThisClass;
  4. @Injectable
  5. String names;
  6. @Injectable
  7. String username;
  8. @Injectable
  9. char[] password = {'t', 'e', 's', 't', 's'};;
  10. @Injectable
  11. String destinationName;
  12. @Test(description = "Verify that sendMessageAbstractMethod is called")
  13. public void testSendMessage(@Mocked ObjectMessage message) throws Exception {
  14. testThisClass.sendMessage(message);
  15. new Verifications(testThisClass){{
  16. testThisClass.sendMessageAbstractMethod((Object) any);
  17. times = 1;
  18. }};
  19. }

}

展开查看全部
k2fxgqgv

k2fxgqgv2#

用间谍解决了这个问题

  1. public class TestThisClassTest {
  2. @Injectable
  3. String names;
  4. @Injectable
  5. String username;
  6. @Injectable
  7. char[] password = {'t', 'e', 's', 't', 's'};;
  8. @Injectable
  9. String destinationName;
  10. @Test
  11. public void testSendMessage(@Mocked ObjectMessage message) throws Exception {
  12. TestThisClass abstractImpl = spy(new TestThisClass());
  13. doNothing().when(abstractImpl).sendMessageAbstractMethod(any());
  14. abstractImpl.sendMessage(message));
  15. new Verifications(){{
  16. verify(abstractImpl, times(1)).sendMessage(any());
  17. }};
  18. }
  19. }
展开查看全部

相关问题