【发布时间】: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