저는 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)}
이 질문과 답변을 찾았 지만 문제가 해결되지 않았습니다. 내가 여기서 뭘 잘못하고 있는지 이해하도록 도와 주시겠습니까?