简体   繁体   中英

Why is Mockito.verify failing a different test than the one I placed it in?

Currently developing unit tests for one of my employer's internal Java projects.

While I have extensive unit testing experience has been on the SFDC Apex platform, my background in Java is rather more limited and I'm still wrapping my head around the use of libraries such as Mockito.

At present, I'm being stumped by a very strange test failure involving the use of Mockito.verify. It is very strange because verify seems to be working as expected, but including it causes a different test to fail.

More specifically, I have these two test methods:

    /**
 * Test of getWindowSize method, of class DefaultWindowHandler.
 */
@Test
public void testGetWindowSize() {
    System.out.println("getWindowSize");
    // GIVEN
    final WebDriver mockWebDriver = mock(WebDriver.class);
    final DefaultWindowHandler defaultWindowHandlerUnderTestInstance = new DefaultWindowHandler(mockWebDriver);
    Options mockOptions = mock(Options.class);
    Window mockWindow = mock(Window.class);
    Dimension dummyDimension = new Dimension(1, 2);

    // WHEN
    when(mockWebDriver.manage()).thenReturn(mockOptions);
    when(mockOptions.window()).thenReturn(mockWindow);
    when(mockWindow.getSize()).thenReturn(dummyDimension);

    final Dimension result = defaultWindowHandlerUnderTestInstance.getWindowSize();
    // THEN
    assertEquals(dummyDimension, result);
}

/**
 * Test of maximizeWindow method, of class DefaultWindowHandler.
 */
@Test
public void testMaximizeWindow() {
    System.out.println("maximizeWindow");
    // GIVEN
    final WebDriver mockWebDriver = mock(WebDriver.class);
    final DefaultWindowHandler defaultWindowHandlerUnderTestInstance = new DefaultWindowHandler(mockWebDriver);
    Options mockOptions = mock(Options.class);
    Window mockWindow = mock(Window.class);

    // WHEN
    when(mockWebDriver.manage()).thenReturn(mockOptions);
    when(mockOptions.window()).thenReturn(mockWindow);

    defaultWindowHandlerUnderTestInstance.maximizeWindow();

    // THEN
    // FIXME: If this is not commented out, a testGetWindowSize will fail!
    //Mockito.verify(mockWindow);
}

As the code currently stands, with "Mockito.verify(mockWindow);" commented out, both tests will pass.

If I remove the // comment marks and allow the verification to occur, the second method, testMaximizeWindow() , which contains the verification, will pass as expected.

HOWEVER , the first method, testGetWindowSize() , will now fail with an org.mockito.exceptions.misusing.UnfinishedVerificationException . and then go on to indicate this Mockito.verify() within the testMaximizeWindow() method.

Any ideas how I am misusing verify() or what I need to do to "finish" it?

Verify(mock) doesn't verify something by it self. You should say what exactly you wand to verify. For example, if you create test like:

List mock = mock(ArrayList.class);
verify(mock);

You will get exception:

org.mockito.exceptions.misusing.UnfinishedVerificationException: 
Missing method call for verify(mock) here:
-> at StackOverflowTest.testMockito(StackOverflowTest.java:72)
Example of correct verification:
verify(mock).doSomething()

Example of correct test:

List mock = mock(ArrayList.class);
mock.add(new Object());
verify(mock).add(any());

Where you test that method add was invoked one time (default for verify) with any parameter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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