카테고리 없음

테스트 중 @Service 자동 연결시 UnsatisfiedDependencyException 발생

기록만이살길 2021. 2. 21. 20:17
반응형

테스트 중 @Service 자동 연결시 UnsatisfiedDependencyException 발생

1. 질문(문제점):

저는 Spring을 배우고 있습니다 : 저는 Spring을 기반으로 한 간단한 JUnit 테스트를 가지고 있습니다. 그러나 @Autowired주석이 둘 다에서 작동 할 것으로 예상하기 때문에 한 테스트가 실패하고 다른 테스트가 실패하는 이유를 이해할 수 없습니다.

2 개의 수업이 있습니다.

package it.euphoria.data.service;

import it.euphoria.data.entity.Customer;
import it.euphoria.data.entity.Seller;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.validation.constraints.NotNull;
import java.util.Set;

public interface CustomerRepository extends JpaRepository<Customer, Long> {

    @Query("SELECT cust FROM Customer cust WHERE cust.seller = :seller")
    Set<Customer> getBySeller(@NotNull @Param("seller") Seller seller);

}

및 서비스 클래스 :

package it.euphoria.data.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CustomerService {

    private CustomerRepository customerRepository;

    @Autowired
    public CustomerService(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }

    public CustomerRepository getCustomerRepository() {
        return customerRepository;
    }


}

경우 테스트하는 동안, 나는 autowire하기 CustomerRepository는 잘 작동하지만, 내가 autowire하기 경우 CustomerService나는를 얻을 수UnsatisfiedDependencyException

이것은 다음과 함께 작동하는 테스트입니다 CustomerRepository.

package it.euphoria.data.service;

import it.euphoria.data.entity.Customer;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@DataJpaTest
public class CustomerRepositoryTest {
    @Autowired
    CustomerRepository customerRepository;        

    @Test
    public void getBySeller() {
        //test things...
    }
}

그리고 이것은 CustomerService유일한 차이점 이있는 깨진 것입니다 .

package it.euphoria.data.service;

import it.euphoria.data.entity.Customer;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@DataJpaTest
public class CustomerRepositoryTest {
    @Autowired
    CustomerService customerService; //<-- The error is here    

    @Test
    public void getBySeller() {
        //test things...
    }
}

이것은 stacktrace입니다.

org.springframework.beans.factory.UnsatisfiedDependencyException : 'it.euphoria.data.service.CustomerRepositoryTest'라는 이름의 빈 생성 오류 : 'customerService'필드를 통해 표현 된 불만족 종속성; 중첩 된 예외는 org.springframework.beans.factory.NoSuchBeanDefinitionException입니다. 'it.euphoria.data.service.CustomerService'유형의 한정 빈이 없습니다. 자동 연결 후보로 한정되는 빈이 1 개 이상 필요합니다. 종속성 주석 : {@ org.springframework.beans.factory.annotation.Autowired (required = true)}

이 질문과 답변을 찾았 지만 문제가 해결되지 않았습니다. 내가 여기서 뭘 잘못하고 있는지 이해하도록 도와 주시겠습니까?

2. 해결방안:

@DataJpaTest 데이터베이스 작업과 관련된 빈을로드하고, 저장소 및 더 낮은 수준의 항목 (DataSources, 트랜잭션 관리자 등)을 읽습니다.

이러한 테스트는 SQL 쿼리 및 DAO에있는 모든 코드의 정확성을 확인하는 데 사용됩니다.

어떤 경우에도 전체 애플리케이션을로드하지 않고 일부만로드합니다.

이제 서비스는 일반적으로 비즈니스 로직을 작성하고 데이터베이스와 직접 상호 작용하지 않는 곳입니다 (DAO, 저장소 등을 통해서만).

그래서 봄은 서비스를로드하지 않습니다. @DataJpaTest

65885402
반응형