WireMock
Gatting Start
-
dependency : testImplementation "com.github.tomakehurst:wiremock-standalone"
-
在本地启动一个Mock Server
- @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
- matching : String
- matchingJsonPath : Json
- containing
- equalToXml
- matchingXPath ?
JUnit 5 : jupiter
Getting Start
-
Depedency
- "org.junit.jupiter:junit-jupiter-api",
- "org.junit.jupiter:junit-jupiter-engine",
- "io.mockk:mockk:1.8.13.kotlin13"
-
Class Annonation
- @ExtendWith(MockKExtension::class)
- @TestInstance(TestInstance.Lifecycle.PER_CLASS)
-
Method Annonation
- @Test
- @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