【发布时间】:2019-05-21 21:42:30
【问题描述】:
我正在尝试测试 Spring JPA 存储库接口以确保我的映射正确。我的实体扩展了一个带有注释的基本实体..
@EntityListeners(BaseEntityEventListener.class)
@MappedSuperclass
public abstract class BaseEntity {...
事件侦听器填充一些审计属性..
public class BaseEntityEventListener {
@PrePersist
public void onPreInsert(BaseEntity baseEntity){
MyUserPrincipal principal = (MyUserPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = principal.getUsername();
baseEntity.setCreationUser(username);
Timestamp ts = new Timestamp(System.currentTimeMillis());
baseEntity.setCreationDate(ts);
}...
这没关系,但是当我想测试存储库时,我得到了 SecurityContextHolder 的空指针。
@RunWith(SpringRunner.class)
@SpringBootTest
public class RepositoryTest {
@Autowired private MyRepository myRepo;
@Test
public void testSaveEntity() throws Exception {
Entity entity = new Entity(TEST_ID);
myRepo.save(entity);
}...
当从测试中调用事件侦听器类时,未设置安全上下文。我曾尝试使用 @WithMockUser 但这似乎不起作用。我可以在服务中包装对安全上下文的调用,然后在我的集成测试中以某种方式模拟这个调用。如果这是一个选项,我如何在实体侦听器上设置模拟。当我使用 @CreatedBy 和 @CreatedDate 时,安全上下文不是问题,但我需要手动使用 @PreInsert 出于单独的原因。
【问题讨论】:
标签: spring-boot spring-security spring-data-jpa