Java @DirtiesContext不适用于Spring Boot和Kotlin
我试图在SpringBoot中运行一些集成测试。为此,我认为我们应该在每个测试运行后使用Java @DirtiesContext不适用于Spring Boot和Kotlin,java,spring-boot,kotlin,Java,Spring Boot,Kotlin,我试图在SpringBoot中运行一些集成测试。为此,我认为我们应该在每个测试运行后使用@DirtiesContext清理数据库 我现在试过几种方法,但都有同样的效果 这是我的设置: 弹簧护套1.5.10.1释放 SQL Server 2016 Kotlin 1.2.21 这是完整的POM: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xm
@DirtiesContext
清理数据库
我现在试过几种方法,但都有同样的效果
这是我的设置:
- 弹簧护套1.5.10.1释放
- SQL Server 2016
- Kotlin 1.2.21
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.brabantia.esb</groupId>
<artifactId>log-db</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>log-db</name>
<description>Log database application for TIBCO ESB</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<kotlin.version>1.2.21</kotlin.version>
<vaadin.version>8.3.0</vaadin.version>
<spring.boot.version>1.5.10.RELEASE</spring.boot.version>
<jupyter.version>5.1.0</jupyter.version>
<surefire.version>2.19.1</surefire.version>
<!-- Properties are prefixed with flyway. -->
<flyway.user>esb_logging_user</flyway.user>
<flyway.password>esb_logging_user</flyway.password>
<!-- List are defined as comma-separated values -->
<flyway.schemas>dbo</flyway.schemas>
<!-- Individual placeholders are prefixed by flyway.placeholders. -->
<!--<flyway.placeholders.keyABC>valueXYZ</flyway.placeholders.keyABC>-->
<!--<flyway.placeholders.otherplaceholder>value123</flyway.placeholders.otherplaceholder>-->
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator-docs</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<!--<dependency>-->
<!--<groupId>com.h2database</groupId>-->
<!--<artifactId>h2</artifactId>-->
<!--<scope>runtime</scope>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>5.0.7</version>
</dependency>
<dependency>
<groupId>com.tibco</groupId>
<artifactId>jms</artifactId>
<version>8.4.1</version>
</dependency>
<dependency>
<groupId>com.tibco</groupId>
<artifactId>tibjms</artifactId>
<version>8.4.1</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-bom</artifactId>
<version>${vaadin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
<jvmTarget>1.8</jvmTarget>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources/jaxb</outputDirectory>
<!-- The package of your generated sources -->
<packageName>com.brabantia.esb.logdb.schema</packageName>
<sources>
<source>src/main/resources/schemas/BrabantiaMain.xsd</source>
</sources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>5.0.7</version>
<configuration>
<user>esb_logging_user</user>
<password>esb_logging_user</password>
<url>jdbc:sqlserver://localhost;database=esb_logging</url>
<schemas>
<schema>schema1</schema>
<schema>schema2</schema>
<schema>schema3</schema>
</schemas>
<placeholders>
<keyABC>valueXYZ</keyABC>
<otherplaceholder>value123</otherplaceholder>
</placeholders>
</configuration>
</plugin>
</plugins>
</build>
</project>
LogMessageContent实体
存储库
存储库都非常标准
@Transactional
internal interface LogRepository : CrudRepository<LogEntity, Long>
@Transactional
internal interface LogReferenceRepository : CrudRepository<LogReferenceEntity, Long>
@Transactional
internal interface LogMessageContentRepository : CrudRepository<LogMessageContentEntity, Long>
不带任何参数的类上的@DirtiesContext
使用classMode
将实例保存到数据库的测试方法的@DirtiesContext
使用methodMode
我也尝试了一些其他的东西,但我有一种感觉,我离得不远了
有人能给我一个正确的方向吗?您不需要使用
@DirtiesContext
来清除测试之间的数据库更改,尽管有时可能需要在测试类之间错误地共享应用程序上下文
相反;您应该将测试标记为@Transactional
。这将确保每个测试方法都由Spring事务包装,不管结果如何,该事务都将回滚
@Test
@Transactional
fun `'addLog' should return the created entity`() {
val result = service.addLog(logCreateDTO = LogCreateDTO())
assertThat(result).isNotNull()
assertThat(result.id).isNotNull()
}
您不需要使用
@DirtiesContext
来清除测试之间的数据库更改,尽管有时可能需要在测试类之间错误地共享应用程序上下文
相反;您应该将测试标记为@Transactional
。这将确保每个测试方法都由Spring事务包装,不管结果如何,该事务都将回滚
@Test
@Transactional
fun `'addLog' should return the created entity`() {
val result = service.addLog(logCreateDTO = LogCreateDTO())
assertThat(result).isNotNull()
assertThat(result.id).isNotNull()
}
谢谢你
@Transactional
修复了它,谢谢你<代码>@Transactional修复了它
@Transactional
internal interface LogRepository : CrudRepository<LogEntity, Long>
@Transactional
internal interface LogReferenceRepository : CrudRepository<LogReferenceEntity, Long>
@Transactional
internal interface LogMessageContentRepository : CrudRepository<LogMessageContentEntity, Long>
@Service
@Transactional
internal class JpaLogService(
val logRepository: LogRepository,
val logReferenceRepository: LogReferenceRepository,
val logMessageContentRepository: LogMessageContentRepository) : LogService {
override fun retrieveLogs(): List<LogDTO> {
return logRepository.findAll().map { it.toDTO() }
}
override fun retrieveLog(id: Long): LogDTO? {
return logRepository.findOne(id)?.toDTO()
}
override fun addLog(logCreateDTO: LogCreateDTO): LogDTO {
val log = LogEntity.fromDto(logCreateDTO)
// Add a reference to each log instance, and persist to the database
log.references.forEach {reference ->
reference.log = log
logReferenceRepository.save(reference)
}
log.messageContent?.let {
it.log = log
logMessageContentRepository.save(it)
}
return logRepository.save(log).toDTO()
}
}
@RunWith(SpringRunner::class)
@SpringBootTest
internal class JpaLogServiceTest {
@Autowired
lateinit var service: LogService
@Autowired
lateinit var repository: LogRepository
@Test
fun `'retrieveLogs' should retrieve empty list if repository doesn't contain entities`() {
assertThat(service.retrieveLogs()).isEmpty()
}
@Test
fun `'addLog' should return the created entity`() {
val result = service.addLog(logCreateDTO = LogCreateDTO())
assertThat(result).isNotNull()
assertThat(result.id).isNotNull()
}
}
@RunWith(SpringRunner::class)
@SpringBootTest
@DirtiesContext
internal class JpaLogServiceTest {
...
}
@RunWith(SpringRunner::class)
@SpringBootTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
internal class JpaLogServiceTest {
...
}
@RunWith(SpringRunner::class)
@SpringBootTest
internal class JpaLogServiceTest {
...
@Test
@DirtiesContext
fun `'addLog' should return the created entity`() {
val result = service.addLog(logCreateDTO = LogCreateDTO())
assertThat(result).isNotNull()
assertThat(result.id).isNotNull()
}
}
@RunWith(SpringRunner::class)
@SpringBootTest
internal class JpaLogServiceTest {
...
@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)
fun `'addLog' should return the created entity`() {
val result = service.addLog(logCreateDTO = LogCreateDTO())
assertThat(result).isNotNull()
assertThat(result.id).isNotNull()
}
}
@Test
@Transactional
fun `'addLog' should return the created entity`() {
val result = service.addLog(logCreateDTO = LogCreateDTO())
assertThat(result).isNotNull()
assertThat(result.id).isNotNull()
}