WireMock

Gatting Start

  • 官网: http://wiremock.org/

  • dependency : testImplementation "com.github.tomakehurst:wiremock-standalone"

  • 在本地启动一个Mock Server

    1. @ClassRule public static WireMockRule wireMockRule = new WireMockRule(options().port(8090).httpsPort(8443).notifier(new ConsoleNotifier(true)));
  • demo

                      val mockServer = WireMockServer(6666)
                      
                      @BeforeAll
                      fun init() {
                          mockServer.start()
                      }
                      @BeforeEach
                     fun cleanUp() {
                          mockServer.resetAll()
                      }
                      @AfterAll
                      fun destroy() {
                          mockServer.stop()
                      }
    
                      @Autowired
                      lateinit var restTemplate: TestRestTemplate
    
                      fun mockResponse(transactionType: TransactionType, response: String) {
                          val mappingBuilder = WireMock.post( mockPath )!!
                          mappingBuilder
                              .withRequestBody( WireMock.containing(transactionType.transactionTypeId) )
                              .willReturn( WireMock.aResponse().withBody( response ) )
                          mockServer.stubFor( mappingBuilder )
                      }
    
  • 在Mock Server上注册一个接口

              wireMockRule.stubFor(
      	post(urlEqualTo(path))
      		.withRequestBody(
      			matchingJsonPath("params[0].cardToken", equalTo("WireMock Response"))
      		)
      		.willReturn(
      			aResponse().withBody("WireMock Response")
      		)
      )
    
  • request body matching

    1. matching : String
    2. matchingJsonPath : Json
    3. containing
    4. equalToXml
    5. matchingXPath ?

JUnit 5 : jupiter

Getting Start

  • Depedency

    1. "org.junit.jupiter:junit-jupiter-api",
    2. "org.junit.jupiter:junit-jupiter-engine",
    3. "io.mockk:mockk:1.8.13.kotlin13"
  • Class Annonation

    1. @ExtendWith(MockKExtension::class)
    2. @TestInstance(TestInstance.Lifecycle.PER_CLASS)
  • Method Annonation

    1. @Test
    2. @BeforeAll
  • Gradle设置

      test {
      if("$test_type"=="UT") {
          useJUnitPlatform {
              includeEngines 'junit-jupiter', 'junit-vintage'
          }
      }
      systemProperties System.properties
      }
    

Embedded DB

  • testImplementation("io.zonky.test:embedded-database-spring-test:1.5.0")
  • @AutoConfigureEmbeddedDatabase