javaspring-boothibernatejunitpersistence.xml

Spring Boot Hibernate createEntityManagerFactory error when I run JUnit test


Iam wrote an API in Spring Boot with Hibernate. It works fine, but when I start to write tests I got an error: Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.

java.lang.IllegalStateException: Failed to load ApplicationContext for [WebMergedContextConfiguration@6c1e40d9 testClass = com.witcher.witcher_api.CharacterRepoTest

Repository:

@Component
@Repository
public class CharacterRepositoryJdbcImpl implements CharacterRepository{

   private EntityManagerFactory emf;
   private EntityManager entityManager;

    @Autowired
    public CharacterRepositoryJdbcImpl() throws ClassNotFoundException {
        System.out.println(getClass().getResource("META-INF/persistence.xml"));
           // I got the error here below
        this.emf = Persistence.createEntityManagerFactory("wticher-pg");
        this.entityManager = emf.createEntityManager();
    }

Test class:

@SpringBootTest
@ActiveProfiles("test")
@AutoConfigureTestDatabase
public class CharacterRepoTest {


    @Autowired
    private CharacterRepository characterRepositoryJdbcImpl;

    @Test
    @Transactional
    public void testGetCharacter(){
        Character testCharacter = characterRepositoryJdbcImpl.findCharacterById(testCharacterId01);
        assertEquals( "Get character by id","character_01", "testname");
    }


persistence.xml

<persistence version="2.2"
             xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
     http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">

    <persistence-unit
            name="wticher-pg">
        <class>com.witcher.witcher_api.model.pojo.User</class>
        <class>com.witcher.witcher_api.model.pojo.Character</class>
        <class>com.witcher.witcher_api.model.pojo.Weapon</class>
        <class>com.witcher.witcher_api.model.pojo.Armor</class>
        <properties>
            <property
                    name="javax.persistence.jdbc.url"
                    value="jdbc:postgresql://localhost:5432/witcher_pg"
            />
            <property
                    name="javax.persistence.jdbc.user"
                    value="witcher_admin"
            />
            <property
                    name="javax.persistence.jdbc.password"
                    value="witcherpass"
            />
            <property
                    name="hibernate.show_sql"
                    value="true"
            />

        </properties>
    </persistence-unit>
</persistence>

I have a persistence.xml file at: src/main/resources/META-INF/persistence.xml and its works fine. I know I need a new application-test.properties file at: test/resources/application-test.properties and I know I need a separate persistence.xml file at: main/resources/META-INF/persistence.xml

But even like this, I got the error when I try to test. I emphasize that the problem only occurs during testing.


Solution

  • I found the solution. I added the JPA dependency. Refactor the repository like this:

    @Component
    @Repository
    public class CharacterRepositoryJdbcImpl implements CharacterRepository{
    
    private final EntityManager entityManager;
    
    @Autowired
    public CharacterRepositoryJdbcImpl( EntityManager entityManager) throws ClassNotFoundException {
        this.entityManager = entityManager;
    
    }
    

    And I deleted the persistence.xml from the test directory.