tjbrn
tjbrn

Reputation: 435

Testing abstract classes with arguments on constructor

I'm trying to learn about tests but I'm facing some problems whilst testing abstract classes. I know I could create a concrete subclass that inherit from Dog, like ConcreteDog, but if I add a new abstract method to Dog, I would have to add a empty method to ConcreteDog so. That would not be so cool I guess.

public abstract class Dog {
  private final int id;

  public Dog(int id) {
    this.id = id;
  }

  public int getId() {
    return id;
  }

  public abstract void makeSound();
}

...

public class DogTest {
  @Test
  public void testGetId() {
    int id = 42;

    // How to pass id to Dog constructor ?
    Dog dog = Mockito.mock(Dog.class, Mockito.CALL_REAL_METHODS);

    assertTrue(dog.getId() == id);
  }
}

What I'm trying to do is somehow call the Mock with the constructor, like

Mockito.mock(Dog(id).class, Mockito.CALL_REAL_METHODS);

I don't know if it's possible with mockito, but there is a way of doing that using mockito or another tool ?

Upvotes: 25

Views: 15082

Answers (3)

Aunmag
Aunmag

Reputation: 962

You may just do this:

Mockito.mock(Dog.class, Mockito.withSettings()
        .useConstructor(999)
        .defaultAnswer(Mockito.CALLS_REAL_METHODS)
);

Where 999 - is any integer for id argument. So you don't have to inherit your abstract class anymore. You also may pass as many arguments as constructor needed e.g.:

.useConstructor(new Object(), "my string", 5.5, null)

Upvotes: 40

UserF40
UserF40

Reputation: 3601

You can set the field using reflection if you need to, this essentially replicates how the constructor is setting the member fields.

Dog dog = Mockito.mock(Dog.class, Mockito.CALL_REAL_METHODS);
ReflectionTestUtils.setField(dog , "id", 42);

Upvotes: 5

Gaspar
Gaspar

Reputation: 159

Since Dog is an abstract class I would recommend to first make a subclass.

class TestDog extends Dog {
...
}

Then you need to create a mock of TestDog and define its behavior. Then when you create an instance of TestDog.class return the mock of TestDog, for example...

TestDog mock_dog = mock(TestDog.class); 
when(mock_dog.getId()).thenReturn(99);
whenNew(TestDog.class).withArguments(anyInt()).thenReturn(mock_dog);

Give this a try and let me know

Upvotes: 4

Related Questions