我正在尝试使用@DataJpaTest在存储库上进行测试,但是@Autowired不起作用--我已经尝试过使用@DataJPATest查找junit5的示例,但我没有找到它
我尝试添加其他依赖项,我使用了@SpringTest,它成功了,但我想使用@DataJpaTest
package com.projetoSpring.catalog.repositories;
import com.projetoSpring.catalog.model.Product;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import java.util.Optional;
@DataJpaTest
public class ProductRepositoryTests {
@Autowired
private ProductRepository repositorys;
@Test
public void deleteShouldDeleteObjectWhenIdExists() {
long exintingId = 1L;
repositorys.deleteById(exintingId);
Optional<Product> result = repositorys.findById(1L);
Assertions.assertFalse(result.isPresent());
}
}org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.projetoSpring.catalog.repositories.ProductRepositoryTests': Unsatisfied dependency expressed through field 'repositorys'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.projetoSpring.catalog.repositories.ProductRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}发布于 2022-11-30 17:45:09
我设法解决了这个问题,我将spring更新到2.7.5版本,并验证了测试依赖关系没有正确下载,我重做了pom.xml,并且它工作了。
https://stackoverflow.com/questions/74628206
复制相似问题