【发布时间】:2017-01-04 15:14:39
【问题描述】:
我正在尝试更新我的测试用例以使用@DataJpaTest。但是,我遇到了一些似乎与 Spring Security 相关的问题。下面是一个测试类的例子。
@RunWith(SpringRunner.class)
@DataJpaTest
public class ExampleRepositoryTest {
@Rule
public final ExpectedException exception = ExpectedException.none();
@Inject
private ExampleRepository repository;
@Test
public void test() throws Exception {
...
}
由于缺少 bean org.springframework.security.config.annotation.ObjectPostProcessor,我不断收到错误 java.lang.IllegalStateException: Failed to load ApplicationContext。
该项目是一个具有 Spring 安全性的 RESTful 应用程序。原始测试用例使用@SpringBootTest 创建了一个完整的 Spring Boot 上下文。 @DataJpaTest 应该可以帮助我测试 JPA 切片,这正是我想要的。
任何帮助将不胜感激。我错过了什么?
【问题讨论】:
-
你有想过这个吗?
-
@pacoverflow 抱歉,但我从未发现问题所在。我只是恢复到旧的
@SpringBootTest -
对于遇到此问题的其他人,我可以通过将
@DataJpaTest注释替换为以下注释来解决它:@SpringBootTest(classes={NameOfTestClass.class})、@AutoConfigureDataJpa、@AutoConfigureTestDatabase、@AutoConfigureTestEntityManager,@Transactional,@EntityScan("model.class.package.name") -
我也一直在尝试解决此问题,并尝试按照您的设置进行操作,但似乎遇到了一些问题。如果您使用 H2,DataJpaTest 将自动运行休眠 DDL 语句,但在这种情况下,它似乎不起作用,进一步看,似乎 EntityScan 注释没有得到尊重。有什么建议吗?
标签: java testing spring-security spring-boot