I am getting NPE while stubbing the method using Junit5 and Mockito

Viewed 62
@ExtendWith(MockitoExtension.class)
public class dmeoStatusServiceTest {

    @InjectMocks
    DemoService demoService;

    @Mock
    DemoRepository demoRepository;

    @Test
    public void shouldDetailedStatus() {
        Optional<Tenant> tenantOptional = Optional.of(createTenantMockObject());
        when(demoRepository.findByMtuNumber("MTU2")).thenReturn(tenantOptional);
    
        demoService.detailedStatus("Deep Shah", "MTU2");

        verify(demoRepository, times(1)).findByMtuNumber("MTU2");
       }
    }
1 Answers

You need to ensure you are using Junit 5 @Test annotation

import org.junit.jupiter.api.Test;

// not

import org.junit.Test;
Related