如何在Spring引导单元测试中使数据存储库可用?

时间:2015-06-18 14:12:31

标签: spring-boot spring-data-jpa

我一直在我正在尝试实施的存储库上获取NPE。

这是回购:

public interface EmployeeRepository extends CrudRepository<Employee, Long> {

    Employee findByEmployeeId(String employeeId);
}

我只是尝试进行简单的集成测试,以确保应用程序正确连接:

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只是注释的集合:

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

我可以提供更多有用的信息吗?感谢。

1 个答案:

答案 0 :(得分:2)

从您的回购字段中删除关键字static。静态字段不是自动装配的。

相关问题