himanshu_942
himanshu_942

Reputation: 69

Getting UnsatisfiedDependencyException Exception in spring

I am currently learning how to do unit testing in spring, I am getting the following error while running the test although I checked everything is fine every @Component is being scanned, I think I am getting this error because of @DataJpaTest can someone help me with it? thanks.

Error :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-07-16 12:46:11.516 ERROR 3492 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Failed to replace DataSource with an embedded database for tests. If you want an embedded database please put a supported one on the classpath or tune the replace attribute of @AutoConfigureTestDatabase.

Test Class

package com.example.SpringBootBasics.repository;

import com.example.SpringBootBasics.entity.Department;
import org.junit.jupiter.api.BeforeEach;
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 static org.junit.jupiter.api.Assertions.*;

@DataJpaTest
class DepartmentRepositoryTest {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private TestEntityManager entityManager;

    @BeforeEach
    void setUp() {
        Department department =
                Department.builder()
                        .departmentName("ME")
                        .departmentAddress("Bangalore")
                        .departmentCode("ME-07")
                        .departmentId(1L)
                        .build();

        entityManager.persist(department);
    }

    @Test
    public void whenFindById_thenReturnDepartment(){
        Department department = departmentRepository.findById(1L).get();

        assertEquals(department.getDepartmentName(), "ME");

    }
}

Upvotes: 1

Views: 672

Answers (1)

Timo
Timo

Reputation: 89

Hints are the last two sentences in the very long exception:

If you want an embedded database please put a supported one on the classpath or tune the replace attribute of @AutoConfigureTestDatabase.

Adding h2 dependency for tests should make it run:

<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <scope>test</scope>
</dependency>

Upvotes: 1

Related Questions