Mathew Alden
Mathew Alden

Reputation: 1680

Test annotated with @DataJpaTest not autowiring field annotated with @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?

Upvotes: 4

Views: 4083

Answers (2)

fap
fap

Reputation: 683

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). Since @JpaTest is already annotated with @ExtendsWith you don't have to actually include it though, see https://stackoverflow.com/a/65359510/4266296.

Upvotes: 2

Ananthapadmanabhan
Ananthapadmanabhan

Reputation: 6216

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.

Upvotes: 2

Related Questions