-
Notifications
You must be signed in to change notification settings - Fork 24
/
HomeControllerWebTest.kt
43 lines (37 loc) · 1.48 KB
/
HomeControllerWebTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.liuwill.demo.kotlinboot.test.acceptence
import com.liuwill.demo.kotlinboot.SpringBootKotlinApplication
import org.hamcrest.Matchers.*
import org.junit.Assert.assertThat
import org.junit.runner.RunWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import org.springframework.boot.context.embedded.LocalServerPort
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.beans.factory.annotation.Autowired
import org.junit.Before
import org.junit.Test
import java.net.URL
/**
* Created by liuwill<liuwill@live.com> on 2017/3/17.
*/
@RunWith(SpringJUnit4ClassRunner::class) // SpringJUnit支持,由此引入Spring-Test框架支持!
@SpringBootTest(classes = arrayOf(SpringBootKotlinApplication::class), webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class HomeControllerWebTest {
@LocalServerPort
private val port: Int = 0
private var base: URL? = null
@Autowired
private val restTemplate: TestRestTemplate? = null
@Before
@Throws(Exception::class)
fun setUp() {
this.base = URL("http://localhost:$port/index.html")
}
@Test
@Throws(Exception::class)
fun getIndex() {
val response = restTemplate?.getForEntity(base.toString(), String::class.java)
assertThat(response?.statusCodeValue, equalTo(200))
assertThat(response?.body, containsString("<title>Spring Boot Kotlin</title>"))
}
}