Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-5979

Annotated Mockito objects should be initialized

    XMLWordPrintable

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Initialize mocks before using them.
    • Highlighting:
      Hide

      Primary:
      The first @Mock@Spy@Captor@InjectMocks annotation

      No need for secondaries. They won't provide any additional value.

      Show
      Primary: The first @Mock ,  @Spy ,  @Captor ,  @InjectMocks annotation No need for secondaries. They won't provide any additional value.
    • Default Severity:
      Blocker
    • Impact:
      High
    • Likelihood:
      High
    • Covered Languages:
      Java
    • Irrelevant for Languages:
      ABAP, APEX, C#, C, C++, Cobol, CSS, Flex, Go, HTML, JavaScript, Kotlin, Objective-C, PHP, PL/I, PL/SQL, Python, RPG, Ruby, Rust, Scala, Solidity, Swift, T-SQL, TypeScript, VB.Net, VB6, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      2min
    • Analysis Level:
      Semantic Analysis
    • Analysis Scope:
      Test Sources

      Description

      Objects annotated with Mockito annotations @Mock@Spy@Captor, or @InjectMocks need to be initialized explicitly.

      There are several ways to do this:

      • Call MockitoAnnotations.openMocks(this) or MockitoAnnotations.initMocks(this) in a setup method
      • Annotate test class with @RunWith(MockitoJUnitRunner.class) (JUnit 4)
      • Annotate test class with @ExtendWith(MockitoExtension.class) (JUnit 5 Jupiter)
      • Use @Rule public MockitoRule rule = MockitoJUnit.rule();

      Test using uninitialized mocks will fail.

      Note that this only applies to annotated Mockito objects. It is not necessary to initialize objects instantiated via Mockito.mock() or Mockito.spy().

      This rule raises an issue when a test class uses uninitialized mocks.

      Noncompliant Code Example

      public class FooTest { // Noncompliant: Mockito initialization missing
        @Mock private Bar bar;       
           
        @Spy private Baz baz;          
      
        @InjectMocks private Foo fooUnderTest; 
      
        @Test
        void someTest() {
          // test something ...
        }
      
        @Nested
        public class Nested {
          @Mock
          private Bar bar;
        }
      

      Compliant Solution

      @RunWith(MockitoJUnitRunner.class) 
      public class FooTest {
        @Mock private Bar bar;           
        // ...
      }
      
      @ExtendWith(MockitoExtension.class)
      public class FooTest {
        @Mock private Bar bar;           
        // ...
      }
      
      public class FooTest {
        @Rule
        public MockitoRule rule = MockitoJUnit.rule(); 
      
        @Mock private Bar bar;           
        // ...
      }
      
      public class FooTest {
        @Mock private Bar bar;           
        // ...
      
        @BeforeEach
        void setUp() {
          MockitoAnnotations.openMocks(this);
        }
        // ...
      }
      
      public class FooTest {
        @Mock private Bar bar;           
        // ...
      
        @Before
        void setUp() {
          MockitoAnnotations.initMocks(this);
        }
        // ...
      }
      
      @ExtendWith(MockitoExtension.class)
      public class FooTest {
        @Nested
        public class Nested {
          @Mock
          private Bar bar;
        }
      }
      

      See

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              nicolas.harraudeau Nicolas Harraudeau (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated: