测试时自动连接@service时出现不满足的依赖异常

qvsjd97n  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(303)

我在学习spring:我有一个简单的基于spring的junit测试,但是我不明白为什么一个测试失败而另一个没有,因为我期望 @Autowired 注解在两者中都起作用
我有两门课:

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...
    }
}

这是stacktrace:
org.springframework.beans.factory.unsatifieddependencyException:创建名为“it.ephoria.data.service.customerrepositorytest”的bean时出错:通过字段“customerservice”表示的未满足的依赖关系;嵌套异常为org.springframework.beans.factory.nosuchbeandefinitionexception:没有类型为“it.ephoria.data.service.customerservice”的限定bean可用:至少需要1个符合autowire候选的bean。依赖项注解:{@org.springframework.beans.factory.annotation.autowired(required=true)}
我找到了这个问题和答案,但没有解决问题。你能帮我理解我做错了什么吗?

bxgwgixi

bxgwgixi1#

@DataJpaTest 加载与数据库、读取存储库和更低级的东西(数据源、事务管理器等)的工作相关的bean。
这些测试用于检查sql查询和驻留在dao中的任何代码的正确性。
它在任何情况下都不会加载整个应用程序,而只加载其中的某一部分。
现在,服务是一个通常编写业务逻辑的地方,它不直接与数据库交互(仅通过dao、存储库等)
所以spring不在 @DataJpaTest

相关问题