【问题标题】:UnsatisfiedDependencyException when autowiring @Service while testing测试时自动装配@Service 时出现 UnsatisfiedDependencyException
【发布时间】:2021-04-29 07:05:58
【问题描述】:

我正在学习 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;
    }


}

如果在测试时我自动连接 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: 创建具有名称的 bean 时出错 'it.euphoria.data.service.CustomerRepositoryTest':不满意 通过字段“customerService”表达的依赖关系;嵌套异常 是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 'it.euphoria.data.service.CustomerService' 类型的合格 bean 可用:预计至少有 1 个符合 autowire 条件的 bean 候选人。依赖注解: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我找到了 this question and answer ,但它并没有解决问题。 你能帮我理解我在这里做错了什么吗?

【问题讨论】:

  • 可能是您的存储库没有使用 @Repository 注释吗?和客服类找不到?
  • @jr593 ,即使使用 @Repository 注释我也会遇到同样的错误

标签: java spring spring-boot junit


【解决方案1】:

@DataJpaTest 加载与数据库工作相关的 bean、读取存储库和更多低级别的东西(数据源、事务管理器等)。

这些测试用于检查您的 SQL 查询和驻留在 DAO 中的任何代码的正确性。

在任何情况下它都不会加载整个应用程序,而只会加载其中的一部分。

现在服务是您通常编写业务逻辑的地方,它不直接与数据库交互(仅通过 DAO、存储库等)

所以spring不会在@DataJpaTest中加载服务

【讨论】:

  • 我根本不知道,谢谢。那么在测试期间哪个是用于使弹簧加载@Service 类的正确注释呢?
  • 这取决于测试的目的。如果要加载整个应用程序,请使用@SpringBootTest。如果要加载应用程序的某些部分,请使用@ContextConfiguration 并指定加载类的配置,这实际上是一个广泛的话题...
猜你喜欢
  • 2020-06-07
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-08
  • 1970-01-01
相关资源
最近更新 更多