用@DataJpaTest 注释的测试不是用@Autowired 注释的自动装配字段 [英] Test annotated with @DataJpaTest not autowiring field annotated with @Autowired
问题描述
我有一个包含 Spring Data Jpa 存储库的 Spring Boot 应用程序.我需要围绕这个存储库运行一个单元(或组件?)测试.我对 Spring Data Jpa 没有太多经验.
这是我的测试.这很简单,我无法通过.
import org.junit.Test;导入 org.springframework.beans.factory.annotation.Autowired;导入 org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;导入静态 org.junit.Assert.assertNotNull;@DataJpaTest公共类 FooRepositoryTest {@自动连线私人 FooRepository fooRepo;@测试public void notNull(){assertNotNull(fooRepo);}}
这是其他相关的源代码.
import com.fedex.dockmaintenancetool.webservice.types.Foo;导入 org.springframework.data.jpa.repository.JpaRepository;导入 org.springframework.stereotype.Repository;@Repository公共接口 FooRepository 扩展 JpaRepository{}
和
import javax.persistence.Entity;@实体公共类 Foo {}
我只是试图将 Jpa 存储库自动连接到测试中,但我不能.显然,我误解了 Spring Boot 工作方式的一些细微差别.但即使经过一些教程,我也无法弄清楚我错过了什么.有人能帮我解决这个问题吗?
当您使用注释 @DataJpaTest
时,意味着您正在尝试仅测试存储库层.该注解用于测试 JPA 存储库,并与 @RunWith(SpringRunner.class)
结合使用以启用填充应用程序上下文.@DataJpaTest
注释禁用完全自动配置并仅应用与 JPA 测试相关的配置.因此,@fap siggested 使用它:
@RunWith(SpringRunner.class)@DataJpaTest公共类 FooRepositoryTest {@自动连线私人 FooRepository fooRepo;@测试public void notNull(){assertNotNull(fooRepo);}}
当您使用注释 @RunWith(SpringRunner.class)
时,SpringRunner 支持加载 Spring ApplicationContext 并将 bean @Autowired
加载到您的测试实例中.>
I have a Spring Boot app which contains a Spring Data Jpa repository. I need to run a unit (or component?) test around this repository. I do not have a lot of experience with Spring Data Jpa.
Here's my test. It's trivially simple, and I cannot get it to pass.
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import static org.junit.Assert.assertNotNull;
@DataJpaTest
public class FooRepositoryTest {
@Autowired
private FooRepository fooRepo;
@Test
public void notNull(){
assertNotNull(fooRepo);
}
}
Here's the other relevant source code.
import com.fedex.dockmaintenancetool.webservice.types.Foo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface FooRepository extends JpaRepository<Foo, Long> {
}
and
import javax.persistence.Entity;
@Entity
public class Foo {
}
I am just trying to get a Jpa repo autowired into a test, and I can't. Clearly I'm misunderstanding some small nuance of how Spring Boot works. But even after going through some tutorials, I cannot figure out what I'm missing. Could anyone help me with this?
When you use the annotation @DataJpaTest
, it means that you are trying to test only the repository layer. The annotation is used to test JPA repositories and is used in combination with @RunWith(SpringRunner.class)
to enable populating the application context. The @DataJpaTest
annotation disables full auto-configuration and applies only configuration relevant to JPA tests.So as @fap siggested use it like :
@RunWith(SpringRunner.class)
@DataJpaTest
public class FooRepositoryTest {
@Autowired
private FooRepository fooRepo;
@Test
public void notNull(){
assertNotNull(fooRepo);
}
}
When you use the annotation @RunWith(SpringRunner.class)
the SpringRunner provides support for loading a Spring ApplicationContext and having beans @Autowired
into your test instance.
这篇关于用@DataJpaTest 注释的测试不是用@Autowired 注释的自动装配字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!