在Spring Boot应用程序中,我想使用启用的审核(@EnableJpaAuditing)测试(JUnit 5)持久层。
我使用Liquibase来设置H2数据库并将Hibernate作为JPA实现。

@Configuration
//@EnableTransactionManagement
@EnableJpaAuditing
//@EnableJpaRepositories
public class MyPersistenceConfig {
}


我的实体具有以下字段:

@CreatedDate
@Column(name = "CREATED_AT", updatable = false)
private Instant createdAt;

@CreatedBy
@Column(name = "CREATED_BY", updatable = false)
private String createdBy;

@CreatedDate
@Column(name = "LAST_MODIFIED_AT")
private Instant lastModifiedAt;

@CreatedBy
@Column(name = "LAST_MODIFIED_BY")
private String lastModifiedBy;


我有以下依赖项:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-core</artifactId>
        <scope>runtime</scope>
        <!--<scope>test</scope>-->
    </dependency>

    <!-- Testing -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>test</scope>
    </dependency>


我尝试了几种注释组合:

@SpringBootTest //(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//@DataJpaTest
@ContextConfiguration(classes = MyPersistenceConfig.class)
@EnableAutoConfiguration
//@SecurityTestExecutionListeners
//@Import(SpringBootWebSecurityConfiguration.class)
@WithMockUser(username = "test", password = "test", roles = "USER")
@ExtendWith(SpringExtension.class)
class MyRepositoryTest {

    @Autowired
    private MyRepository testee;

...
}


但是无论我如何尝试,要么存储库为空(自动装配),要么在插入条目时出现异常:

NULL not allowed for column "CREATED_BY"; SQL statement:


我想我需要一个SecurityContext(当前不适用于自动装配)。

提供与审核和@WithMockUser一起使用的模拟SecurityContext的最简单方法是什么?

最佳答案

@Before
public void setup() {

    User user = userService.findByEmail("[email protected]").get();

    SecurityContextHolder.getContext().setAuthentication(new Authentication() {
        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            return null;
        }

        @Override
        public Object getCredentials() {
            return user.getPassword();
        }

        @Override
        public Object getDetails() {
            return user;
        }

        @Override
        public Object getPrincipal() {
            return null;
        }

        @Override
        public boolean isAuthenticated() {
            return true;
        }

        @Override
        public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {

        }

        @Override
        public String getName() {
            return user.getName();
        }
    });

}


也许您可以使用@Before注释

关于java - Spring Data + SpringBootTest:如何为JPA审计模拟SecurityContext?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55212389/

10-10 04:09