spring-bootmockitojunit5

What's wrong that always returns null for the repository and the test fails?


my test method always fails, and I always get the error below:

java.lang.NullPointerException: Cannot invoke "com.priserp.springapipeta.repository.EmpresaRepository.findAll()" because "this.empresaRepository" is null

at com.priserp.springapipeta.service.EmpresaService.getAllEmpresas(EmpresaService.java:53) at com.priserp.springapipeta.unit.service.EmpresaServiceTest.should_Return_All_Empresas(EmpresaServiceTest.java:66) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at java.base/java.util.ArrayList.forEach(ArrayList.java:1596) at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

my service:

@Service
public class EmpresaService {

    private final EmpresaRepository empresaRepository;

    public List<Empresa> getAllEmpresas() {
        return empresaRepository.findAll();
    }

}

@Repository
public interface EmpresaRepository extends JpaRepository<Empresa, Long> {
    Optional<Empresa> findByRazaoSocial(String name);
    Optional<Empresa> findByCnpj(String cnpj);
}

my test class, I want to check if the method returns a list but this list is always empty or index out of bounds

@RunWith(MockitoJUnitRunner.class)
@SpringBootTest(classes = PetaApiApplication.class)
class EmpresaServiceTest {
    @Mock
    EmpresaRepository empresaRepository;

    @InjectMocks
    EmpresaService empresaService = new EmpresaService(empresaRepository);

    private final List<Empresa> empresas = new ArrayList<>();

    @Test
    public void should_Return_All_Empresas() {
        Empresa empresa1 = new Empresa(1L, "EMPRESA 1", "12345678");
        Empresa empresa2 = new Empresa(2L, "EMPRESA 2", "456");
        empresas.add(empresa1);
        empresas.add(empresa2);    
        when(empresaRepository.findAll()).thenReturn(empresas);

        List<Empresa> foundEmpresas = this.empresaService.getAllEmpresas();

        assertNotEquals(null, foundEmpresas);

        assertEquals("EMPRESA 1", foundEmpresas.get(0).getRazaoSocial());

        System.out.println(foundEmpresas.size());
        System.out.println(empresas.size());

    }

}

Solution

  • Update Used code from @GeorgiiLvov instead, and asked OP to accept his answer instead. Will delete this answer when that is done.

    I believe you're trying to unit test EmpresaService, hence this is not a Spring Boot test. Please try to change your test class like this:

    @ExtendWith(MockitoExtension.class)
    class EmpresaServiceTest {
    
        @Mock
        EmpresaRepository empresaRepository;
    
        @InjectMocks
        EmpresaService empresaService; // Mockito inits this
    
    
        @Test
        public void should_Return_All_Empresas() {
           // your test code
        }
    }
    

    And as I mentioned in a comment; you'll need a constructor in EmpresaService:

    @Service
    public class EmpresaService {
    
        private final EmpresaRepository empresaRepository;
    
        public EmpresaService(EmpresaRepository empresaRepository) {
           this.empresaRepository = empresaRepository;
        }