3

i have a project with many test files. In one of the test classes i need to mock finall class. As i found out it can be done with MockMaker (link), however this breaks all my other test classes showin as reason:

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);

without Mock-Maker all other tests are fine.

How can i specify to use MockMaker only on a single test class?

Akka Jaworek
  • 1,970
  • 4
  • 21
  • 47

2 Answers2

1

Try using PowerMockito.. it deals well with finals and statics:

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.6.5</version>
    <scope>test</scope>
</dependency>

Mocking a final class:

import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({MyFinalClass.class})
public class MyTest {

    @Test
    public void myFinalClassTest() {
        MyFinalClass finalMock= PowerMockito.mock(MyFinalClass .class);


        Mockito.when(finalMock.toString()(testInput)).thenReturn("abc");

        // Assertions            
    }

}

You use this feature only where it is needed.. in all other places you can keep your original Mockito usage.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
0

You can not mock final class based on this link : https://github.com/mockito/mockito/wiki/FAQ#what-are-the-limitations-of-mockito

see these links:

How to mock a final class with mockito

How to mock a final class with mockito

try to use Power Mockito as follow:

public final class Plane {
    public static final int ENGINE_ID_RIGHT = 2;
    public static final int ENGINE_ID_LEFT = 1;

    public boolean verifyAllSystems() {
        throw new UnsupportedOperationException("Fail if not mocked!");
    }

    public void startEngine(int engineId) {
        throw new UnsupportedOperationException(
                "Fail if not mocked! [engineId=" + engineId + "]");
    }
}

public class Pilot {
    private Plane plane;

    public Pilot(Plane plane) {
        this.plane = plane;
    }

    public boolean readyForFlight() {
        plane.startEngine(Plane.ENGINE_ID_LEFT);
        plane.startEngine(Plane.ENGINE_ID_RIGHT);
        return plane.verifyAllSystems();
    }
}

and test final class:

@PrepareForTest(Plane.class)
public class PilotTest extends PowerMockTestCase {
    @Test
    public void testReadyForFlight() {
        Plane planeMock = PowerMockito.mock(Plane.class);
        Pilot pilot = new Pilot(planeMock);

        Mockito.when(planeMock.verifyAllSystems()).thenReturn(true);

        // testing method
        boolean actualStatus = pilot.readyForFlight();

        Assert.assertEquals(actualStatus, true);
        Mockito.verify(planeMock).startEngine(Plane.ENGINE_ID_LEFT);
        Mockito.verify(planeMock).startEngine(Plane.ENGINE_ID_RIGHT);
    }
}

example link : https://dzone.com/articles/mock-final-class

Community
  • 1
  • 1
M2E67
  • 937
  • 7
  • 23