Spring-Test with JUnit 4, And JUnit 5
Gradle
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation "org.springframework:spring-context:5.2.0.RELEASE"
testImplementation 'junit:junit:4.12' <----------- JUnit 4
testImplementation "org.springframework:spring-test:5.2.0.RELEASE" <------ Spring-Test
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.7.1") <------ JUnit Engine
}
Start a Spring Test
import pkg.JavaConfig
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.ApplicationContext
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
@RunWith(SpringJUnit4ClassRunner::class)
@ContextConfiguration(classes = [JavaConfig::class])
class ApplicationContextTest {
@Autowired lateinit var ctx: ApplicationContext
@Test
fun testApplicationContext() {
// TODO
}
}
also:
class ApplicationContextTest {
@Autowired val ctx: ApplicationContext? = null
@Test
fun testApplicationContext() {
assert(ctx !== null)
}
}
参数化测试
package headfirst_junit
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import java.util.regex.Pattern
object EmailIdUtility {
fun isValid(email: String): Boolean {
val regex =
"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$"
val pattern = Pattern.compile(regex)
val m = pattern.matcher(email)
return m.matches()
}
}
@RunWith(value = Parameterized::class)
class EmailIdValidatorTest(
private val email: String,
private val expected: Boolean)
{
@Test
fun testIsValidEmailId() {
val actual = EmailIdUtility.isValid(email)
assertEquals(expected, actual)
}
companion object {
@JvmStatic
@Parameterized.Parameters(name = "{index}: isValid({0})={1}")
fun data(): Any {
return arrayOf(
arrayOf("mary@testdomain.com", true),
arrayOf("mary.smith@testdomain.com", true),
arrayOf("mary_smith123@testdomain.com", true),
arrayOf("mary@testdomaindotcom", false),
arrayOf("mary-smith@testdomain", false),
arrayOf("testdomain.com", false)
)
}
}
}
迁移到JUnit 5
Gradle
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation "org.springframework:spring-context:5.2.0.RELEASE"
testImplementation "org.springframework:spring-test:5.2.0.RELEASE" <------ Spring-Test
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.1' <----------- JUnit 5
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.1'
}
迁移技巧By Gradle:https://junit.org/junit5/docs/current/user-guide/#migrating-from-junit4-tips
- @RunWith不复存在; 被@ExtendWith取代。
迁移技巧By Spring社区:https://www.baeldung.com/junit-5-migration
- SpringJUnit4ClassRunner -> SpringExtension
@RunWith(SpringJUnit4ClassRunner::class)
@ContextConfiguration(["/app-config.xml", "/test-data-access-config.xml"])
public class SpringExtensionTest {
/*...*/
}
现在,在JUnit 5中,它是一个简单的扩展:
@ExtendWith(SpringExtension::class)
@ContextConfiguration(["/app-config.xml", "/test-data-access-config.xml"])
public class SpringExtensionTest {
/*...*/
}