Joe Essey
Joe Essey

Reputation: 3538

How to make data repository available in spring boot unit tests?

I'm consistently getting NPE on the repository I'm trying to implement.

Here's the repo:

public interface EmployeeRepository extends CrudRepository<Employee, Long> {

    Employee findByEmployeeId(String employeeId);
}

I'm just trying to do a simple integration test to make sure the app is wired correctly:

public class EmployeeRepositoryTest extends BaseIntegrationTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeRepositoryTest.class);

    @Autowired
    private static EmployeeRepository repo;

    @Test
    public void findByEmployeeIdReturnsAppropriateEmployee() {
        Employee e = new Employee("Some", "Person", "0001111");
        repo.save(e);
        assertEquals("Did not find appropriate Employee", e, repo.findByEmployeeId("0001111"));
    }
}

BaseIntegrationTest is just a collection of annotations:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@ActiveProfiles("test")
@IntegrationTest("server.port:0")
public class BaseIntegrationTest {
    @Test
    public void contextLoads() {
    }
}

Can I provide any more helpful information? Thanks.

Upvotes: 0

Views: 72

Answers (1)

dunni
dunni

Reputation: 44555

Remove the keyword static from your repo field. Static fields aren't autowired.

Upvotes: 2

Related Questions