0

I'm trying to write a unit test for a class which extends an abstract class but the tests are still trying to call the real abstract methods. Is there a way to inject an Mocked abstract class and verify that the abstracted method was called?

Test

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
     
    }
}

AbstractClass

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
    }
}
PT_C
  • 1,178
  • 5
  • 24
  • 57
  • Why are you trying to inject an abstract class? By definition, you can't have an object of an abstract class, you can only have instances of non-abstract classes. – Progman Nov 16 '20 at 19:00
  • @Progman I'm trying to inject a Mocked version of the abstract class so that I can record the abstract method is being called. Is there a way to do this without instantiating the abstract class? – PT_C Nov 16 '20 at 19:05

2 Answers2

0

Solved this using a spy

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());
        }};
    }
}
PT_C
  • 1,178
  • 5
  • 24
  • 57
0

This should work:

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;
    }};
}

}

Jeff Bennett
  • 996
  • 7
  • 18