1

I have created a new Spring Boot application using Spring Data. I got an issue when I try to test my JpaRepository:

[ERROR] Failures: 
[ERROR]   EmployeeRepositoryTest.testDeleteEmployee:46 NullPointer
[ERROR]   EmployeeRepositoryTest.testGetEmployee:37 NullPointer
[ERROR]   EmployeeRepositoryTest.testSaveEmployee » Test 
Expected exception of type cla...

java.lang.NullPointerException in the line

repository.save(employee);

where a repository has been defined as:

@Autowired
private EmployeeRepository repository;

Follow my pom.xml and test class:

@ExtendWith({SpringExtension.class})
    @DataJpaTest
    class EmployeeRepositoryTest {

        @Autowired
        private EmployeeRepository repository;

        @Test(expectedExceptions = ResourceNotFoundException.class)
        void testSaveEmployee() throws ResourceNotFoundException {
            Employee employee = new Employee("John", "Smith", "john.smith@email.com");
            repository.save(employee);
            employee = repository.findById(employee.getId()).orElseThrow(() -> new ResourceNotFoundException(""));
            assertNotNull(employee);
            assertEquals(employee.getFirstName(), "John", "The employee firstname should be John");
        }
    }

...

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
   ...

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.13.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.5.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>
...   

Could you please help to understand which is the issue?

Below the EmployeeRepository interface:

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long>{    
}
  • Where is your `EmployeeRepository` interface? – SeverityOne Nov 27 '19 at 15:29
  • Just a simple interface that extends JpaRepository @Repository public interface EmployeeRepository extends JpaRepository { } – Giuseppe Scaramuzzino Nov 27 '19 at 15:30
  • Note sure if this is already answered here: https://stackoverflow.com/questions/48347088/spring-test-with-datajpatest-cant-autowire-class-with-repository-but-with-in – sfiss Nov 27 '19 at 15:51
  • you might need `@SpringBootTest` – Ryuzaki L Nov 27 '19 at 16:16
  • No, it's not working even if I change DataJpaTest with SpringBootTest If I try to run all test with my IDE (intellij), I do not have the issue. All tests are completed without error but if I try to run: mvn clean package or mvn clean test I've got the issue with NullPointer. – Giuseppe Scaramuzzino Nov 27 '19 at 16:20
  • Which version of Spring Boot are you using? – Lorelorelore Nov 27 '19 at 16:25
  • I use 2.2.1.RELEASE – Giuseppe Scaramuzzino Nov 27 '19 at 16:26
  • One problem in your question is that it isn't a [complete and reproducible](https://stackoverflow.com/help/minimal-reproducible-example) example, although that is understandable with JPA. But we cannot take your code and run it, because it's just snippets. What I would wager, though, is that your repository isn't found by Spring. It could be that it lacks an annotation, or that you need to specify what packages are being scanned for repositories. – SeverityOne Nov 28 '19 at 11:35

0 Answers0