I am trying to write some tests for a repository in my spring boot application, however the repository is autowired as null. The code for the test class is as follows:
package jpa.project.repo;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.ContextConfiguration;
import jpa.project.entity.Person;
@EnableAutoConfiguration
@ContextConfiguration(classes = PersonRepo.class)
@DataJpaTest
public class PersonRepoTest {
@Autowired
private PersonRepo personRepoTest;
@Test
public void testPersonRepo() throws Exception {
Person toSave = new Person();
toSave.setPersonId(23);
if (personRepoTest == null) {
System.out.println("NULL REPO FOUND");
}
personRepoTest.save(toSave);
Person getFromDb = personRepoTest.findOne(23);
Assert.assertTrue(getFromDb.getPersonId() == 23);
}
}
The print statement does get printed when I run this file as JUnit test in Eclipse, which confirms the nullpointerexception that then comes. All of my tests are in the same packages as the main application, but the packages are under src/test/java. I tried a few changes with the packaging names, but that didn't help so I don't know what the problem is now. Why is the repo initialized to null?