Error Description: When running the test findByEmail
in the ClientRepositoryTest
class, a NullPointerException
occurs with the message "Cannot invoke org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager.persist(Object)
because com.saajf.manager.eewaapro.ClientRepositoryTest.entityManager
is null."
Code Snippet:
package com.saajf.manager.eewaapro;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import com.saajf.manager.eewaapro.domain.entities.Client;
import com.saajf.manager.eewaapro.infrastructure.repositories.ClientRepository;
@DataJpaTest
class ClientRepositoryTest {
@Autowired
private ClientRepository clientRepository;
private static TestEntityManager entityManager;
@Test
void findByEmail() {
Client client = new Client();
client.setId(1L);
client.setCompanyAddress1("cr 54 # 143a - 90");
client.setCompanyName("cualquier cosa");
client.setCompanyType("Software");
client.setCompanyPhone1("3165476589");
client.setEmail("[email protected]");
client.setIdentification("123456789");
// Guardar el cliente utilizando el entityManager persistente
Client savedClient = entityManager.persist(client);
// Limpiar el entityManager para asegurar que se recargue el cliente desde la
// base de datos
entityManager.clear();
// Realizar la búsqueda del cliente por email
Client foundClient = clientRepository.findByEmail(savedClient.getEmail());
assertThat(foundClient).isEqualTo(savedClient);
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- Parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.11</version>
<relativePath/>
</parent>
<groupId>com.atukaa.eewaapro.manager</groupId>
<artifactId>eewaapro-manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eewaapro-manager</name>
<description>Manager component for the Atukaa Eewaapro Service</description>
<properties>
<java.version>17</java.version>
</properties>
<!-- Dependencies -->
<!-- Add your dependencies here -->
...
<!-- Build -->
<!-- Add your build configuration here -->
...
</project>
Stack Overflow Question: Title: NullPointerException when running JUnit test with TestEntityManager
I'm experiencing a NullPointerException
when running a JUnit test with TestEntityManager
. The error message states: "Cannot invoke org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager.persist(Object)
because com.saajf.manager.eewaapro.ClientRepositoryTest.entityManager
is null."
contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]] [[1;31mERROR[m] [1;31mTests [0;1mrun: [0;1m1[m, Failures: 0, [1;31mErrors: [0;1;31m1[m, Skipped: 0, Time elapsed: 4.892 s[1;31m <<< FAILURE![m - in com.saajf.manager.eewaapro.[1mClientRepositoryTest[m [[1;31mERROR[m] findByEmail Time elapsed: 0.239 s <<< ERROR! java.lang.NullPointerException: Cannot invoke "org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager.persist(Object)" because "com.saajf.manager.eewaapro.ClientRepositoryTest.entityManager" is null at com.saajf.manager.eewaapro.ClientRepositoryTest.findByEmail(ClientRepositoryTest.java:33)
2023-05-15 23:24:32.471 INFO 13068 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
You need to autowire the TestEntityManager
as follows:
@DataJpaTest
class ClientRepositoryTest {
@Autowired
private ClientRepository clientRepository;
@Autowired
private TestEntityManager entityManager;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments