/assertj-mockito

assertj-mockito is a library integrating mockito with assertj's soft assertions.

Primary LanguageJava

Build Status Coverage Status Codacy Badge

assertj-mockito is a library integrating mockito with AssertJ's soft assertions.

Usage

  • JUnit 4

    • Dependency
      • gradle

        testCompile 'org.assertj:assertj-mockito-junit4:1.0-SNAPSHOT'

      • maven
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-mockito-junit4</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>test</scope>
        </dependency>
    • Example
      public class ValidCalculatorTest {
          @Rule
          public AssertJMockitoSoftAssertions softly = new AssertJMockitoSoftAssertions();
          @Rule
          public MockitoRule rule = rule();
      
          @Mock
          private Adder adder;
      
          @Test
          public void test() {
              ValidCalculator calculator = new ValidCalculator(adder);
              when(adder.add(1, 2)).thenReturn(3);
              int result = calculator.add(1, 2);
      
              verify(adder, times(1)).add(1, 2);
              softly.assertThat(result).isEqualTo(3);
              verifyNoMoreInteractions(adder);
          }
      }        
  • JUnit 5

    • Dependency
      • gradle

        testCompile 'org.assertj:assertj-mockito-junit5:1.0-SNAPSHOT'

      • maven
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-mockito-junit5</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>test</scope>
        </dependency>
    • Example
      @ExtendWith(AssertJMockitoExtension.class)
      public class ValidCalculatorTest {
          @Mock
          private Adder adder;
      
          @Test
          public void test(SoftAssertions softly) {
              ValidCalculator calculator = new ValidCalculator(adder);
              when(adder.add(1, 2)).thenReturn(3);
              int result = calculator.add(1, 2);
      
              verify(adder, times(1)).add(1, 2);
              softly.assertThat(result).isEqualTo(3);
              verifyNoMoreInteractions(adder);
          }
      }