Skip to content

Commit ecc91a6

Browse files
authored
Fix kotlin tests (eugenp#1995)
1 parent ef7264c commit ecc91a6

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

‎spring-5-mvc/src/main/kotlin/com/baeldung/springbootkotlin/KotlinDemoApplication.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.baeldung.springbootkotlin
33
import org.springframework.boot.SpringApplication
44
import org.springframework.boot.autoconfigure.SpringBootApplication
55

6-
@SpringBootApplication
6+
@SpringBootApplication(scanBasePackages = arrayOf("com.baeldung.springbootkotlin"))
77
class KotlinDemoApplication
88

99
fun main(args: Array<String>) {

‎spring-5-mvc/src/test/kotlin/springbootkotlin/KotlinDemoApplicationIntegrationTest.kt‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,41 @@ import org.springframework.http.HttpStatus
1313
import org.springframework.test.context.junit4.SpringRunner
1414

1515
@RunWith(SpringRunner::class)
16-
@SpringBootTest(classes = arrayOf(KotlinDemoApplication::class), webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
16+
@SpringBootTest(
17+
classes = arrayOf(KotlinDemoApplication::class),
18+
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
1719
class KotlinDemoApplicationIntegrationTest {
1820

1921
@Autowired
20-
val testRestTemplate: TestRestTemplate? = null
22+
lateinit var testRestTemplate: TestRestTemplate
2123

2224
@Test
23-
fun contextLoads() {
24-
}
25-
26-
@Test
27-
fun testHelloController() {
28-
val result = testRestTemplate?.getForEntity("/hello", String::class.java)
25+
fun whenCalled_thenShouldReturnHello() {
26+
val result = testRestTemplate.withBasicAuth("user", "pass")
27+
.getForEntity("/hello", String::class.java)
2928

3029
assertNotNull(result)
31-
assertEquals(result?.statusCode, HttpStatus.OK)
32-
assertEquals(result?.body, "hello world")
30+
assertEquals(HttpStatus.OK, result?.statusCode)
31+
assertEquals("hello world", result?.body)
3332
}
3433

3534
@Test
36-
fun testHelloService() {
37-
val result = testRestTemplate?.getForEntity("/hello-service", String::class.java)
35+
fun whenCalled_thenShouldReturnHelloService() {
36+
val result = testRestTemplate.withBasicAuth("user", "pass")
37+
.getForEntity("/hello-service", String::class.java)
3838

3939
assertNotNull(result)
40-
assertEquals(result?.statusCode, HttpStatus.OK)
40+
assertEquals(HttpStatus.OK, result?.statusCode)
4141
assertEquals(result?.body, "hello service")
4242
}
4343

4444
@Test
45-
fun testHelloDto() {
46-
val result = testRestTemplate?.getForEntity("/hello-dto", HelloDto::class.java)
45+
fun whenCalled_thenShouldReturnJson() {
46+
val result = testRestTemplate.withBasicAuth("user", "pass")
47+
.getForEntity("/hello-dto", HelloDto::class.java)
4748

4849
assertNotNull(result)
49-
assertEquals(result?.statusCode, HttpStatus.OK)
50+
assertEquals(HttpStatus.OK, result?.statusCode)
5051
assertEquals(result?.body, HelloDto("Hello from the dto"))
5152
}
5253

0 commit comments

Comments
 (0)