JUnit @DataJpaTest org.springframework.beans.factory.Unsatis



我正试图使用@DataJPATest对存储库进行测试,但@Autowired不起作用我已经尝试寻找junit5与@DataJpaTest的例子,但我没有找到它

我尝试添加其他依赖关系,我使用@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)}

我设法解决了这个问题,我将spring更新到2.7.5版本,我验证了测试依赖项不能正确下载,我重新做了pom.xml,它工作了。

最新更新