• 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 {
        /*...*/
    }
    
  • 相关阅读:
    Django U2 模型
    Django U0 使用Django
    Django H2 文档查看
    python模块--time模块/os模块/sys模块
    python模块-logging和collections以及random模块
    python-hashlib模块configparser模块logging模块
    python模块--序列化
    python面向对象的特殊方法和单例模式
    python类属性和类方法以及静态方法和反射
    python面向对象的三个特性
  • 原文地址:https://www.cnblogs.com/develon/p/14501161.html
Copyright © 2020-2023  润新知