测试时自动装配 @Service 时出现 UnsatisfiedDependencyException
UnsatisfiedDependencyException when autowiring @Service while testing
我正在学习 Spring:我有一个基于 Spring 的简单 JUnit 测试,但我不明白为什么一个测试失败而另一个没有,因为我期待 @Autowired
注释在它们中都起作用
我有 2 个 classes:
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);
}
和一个服务 class:
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;
}
}
如果,在测试时,我自动装配 CustomerRepository
它工作正常,但如果我自动装配 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...
}
}
这是堆栈跟踪:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name
'it.euphoria.data.service.CustomerRepositoryTest': Unsatisfied
dependency expressed through field 'customerService'; nested exception
is org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type 'it.euphoria.data.service.CustomerService'
available: expected at least 1 bean which qualifies as autowire
candidate. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
我找到了 ,但没有解决问题。
你能帮我理解我做错了什么吗?
@DataJpaTest
加载与数据库工作相关的 bean,读取存储库和更多低级内容(数据源、事务管理器等)。
这些测试用于检查您的 SQL 查询和驻留在 DAO 中的任何代码的正确性。
它不会在任何情况下加载整个应用程序,只会加载其中的一部分。
现在服务是您通常编写业务逻辑的地方,它不直接与数据库交互(仅通过 DAO、存储库等)
因此 spring 不会在 @DataJpaTest
中加载服务
我正在学习 Spring:我有一个基于 Spring 的简单 JUnit 测试,但我不明白为什么一个测试失败而另一个没有,因为我期待 @Autowired
注释在它们中都起作用
我有 2 个 classes:
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);
}
和一个服务 class:
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;
}
}
如果,在测试时,我自动装配 CustomerRepository
它工作正常,但如果我自动装配 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...
}
}
这是堆栈跟踪:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'it.euphoria.data.service.CustomerRepositoryTest': Unsatisfied dependency expressed through field 'customerService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'it.euphoria.data.service.CustomerService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我找到了
@DataJpaTest
加载与数据库工作相关的 bean,读取存储库和更多低级内容(数据源、事务管理器等)。
这些测试用于检查您的 SQL 查询和驻留在 DAO 中的任何代码的正确性。
它不会在任何情况下加载整个应用程序,只会加载其中的一部分。
现在服务是您通常编写业务逻辑的地方,它不直接与数据库交互(仅通过 DAO、存储库等)
因此 spring 不会在 @DataJpaTest