Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

verifyNoMoreInteractions inconsistent behavior #1866

Open
4 of 5 tasks
blueacorn opened this issue Jan 25, 2020 · 1 comment
Open
4 of 5 tasks

verifyNoMoreInteractions inconsistent behavior #1866

blueacorn opened this issue Jan 25, 2020 · 1 comment

Comments

@blueacorn
Copy link

blueacorn commented Jan 25, 2020

The verifyNoMoreInteractions method has an expectation that if will flag unexpected (unchecked) interactions on the related mocks. For Example:

verifyNoMoreInteractions(mocks)

  • will correctly flag any unexpected (unchecked) interactions

However, when it is used with an inOrder context, the code does not behave as expected:

  InOrder inOrder = inOrder(mock, mock2, ...);
  inOrder.verify(mock).firstCheck();
  ... 
  inOrder.verify(mock).lastCheck();
  inOrder.verifyNoMoreInteractions() ;
}
  • correctly flags any unexpected (unchecked) interactions after lastCheck
  • incorrectly ignores any unexpected (unchecked) interactions before lastCheck

Example

The following example illustrates how the placement of verifyNoMoreInteractions changes the behavior. (note the example is written in kotlin, but the affected library is mockito-core)

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.Mock
import org.mockito.junit.jupiter.MockitoExtension

@ExtendWith(MockitoExtension::class)
class ExampleTest {

  @Mock var lateinit instanceA: ClassA
  @Mock var lateinit instanceB: ClassB
  @Mock var lateinit instanceC: ClassC

  val allMocks = Lazy { arrayOf(instanceA, instanceB, instanceC) }

  @Test
  fun example() {
       // WHEN
       instanceA.methodCallA()
       instanceB.methodCallB()
       instanceC.methodCallC()

        // THEN
        inOrder(*allMocks.value) {

             verify(instanceC).methodCallC()

            verifyNoMoreInteractions()            // <-- Does **not** flag unchecked A,B interactions (incorrect)
        }
        verifyNoMoreInteractions(*allMocks.value) // <-- Correctly flags any unchecked interactions
  }
}

Suggestions

Perhaps the [re-use of] method name causes some confusion in expectation:

  • verifyNoMoreInteractions vs. verifyNoOtherInteractions vs. inOrder.verifyLastInteraction vs. inOrder.verifyNoOtherInteractions

Checklist

  • The mockito message in the stacktrace have useful information, but it didn't help
  • The problematic code (if that's possible) is copied here;
    Note that some configuration are impossible to mock via Mockito
  • Provide versions (mockito / jdk / os / any other relevant information)
  • Mockito-2.22
  • JUnit5 5.x
  • Mockito-kotlin = 2.x
  • JDK 1.8
  • Provide a Short, Self Contained, Correct (Compilable), Example of the issue
    (same as any question on stackoverflow.com)
  • Read the contributing guide
@blueacorn blueacorn changed the title inOrder.verifyNoMoreInteractions inconsistent behavior verifyNoMoreInteractions inconsistent behavior Jan 25, 2020
@mockitoguy
Copy link
Member

inOrder.verifyNoMoreInteractions() is different than Mockito.verifyNoMoreInteractions(). Take a look at the Javadoc. If the Javadoc is not clear, please submit a PR ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants