Test annotated with @DataJpaTest not autowiring field annotated with @Autowired

无人久伴 提交于 2021-02-09 11:11:58

问题


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?


回答1:


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.




回答2:


You're missing the @RunWith(SpringRunner.class) annotation that tells JUnit to actually start a Spring application for the test.

Your test class should look like

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;
import static org.junit.Assert.assertNotNull;

@RunWith(SpringRunner.class)
@DataJpaTest
public class FooRepositoryTest {

    @Autowired
    private FooRepository fooRepo;

    @Test
    public void notNull(){
        assertNotNull(fooRepo);
    }
}

The JUnit version used in the question is still JUnit 4. Spring Boot 2.2.0 switches to JUnit5.

With JUnit5 you'll have to use @ExtendWith(SpringExtension.class) instead of @RunWith(SpringRunner.class).



来源:https://stackoverflow.com/questions/58634359/test-annotated-with-datajpatest-not-autowiring-field-annotated-with-autowired

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!