Skip to content

Commit 1aa09b8

Browse files
committed
KTLN-587 added tests
1 parent 01fa047 commit 1aa09b8

File tree

8 files changed

+140
-28
lines changed

8 files changed

+140
-28
lines changed

kotlin-dsl/pom.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@
2020
<groupId>org.springframework.boot</groupId>
2121
<artifactId>spring-boot-starter-web</artifactId>
2222
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-test</artifactId>
26+
</dependency>
2327
</dependencies>
2428

2529
<build>
2630
<sourceDirectory>src/main/kotlin</sourceDirectory>
27-
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
31+
<testSourceDirectory>src/test/java</testSourceDirectory>
2832
<plugins>
2933
<plugin>
3034
<artifactId>kotlin-maven-plugin</artifactId>
35+
<groupId>org.jetbrains.kotlin</groupId>
36+
<version>${kotlin.version}</version>
3137
<executions>
3238
<execution>
3339
<id>compile</id>
@@ -44,8 +50,6 @@
4450
</goals>
4551
</execution>
4652
</executions>
47-
<groupId>org.jetbrains.kotlin</groupId>
48-
<version>${kotlin.version}</version>
4953
<configuration>
5054
<args>
5155
<arg>-Xjsr305=strict</arg>

kotlin-dsl/src/main/kotlin/com/baeldung/functional_dsl/bean_declaration/BeanDeclration.kt renamed to kotlin-dsl/src/main/kotlin/com/baeldung/functionaldsl/beandeclaration/BeanDeclration.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package com.baeldung.functional_dsl.bean_declration
1+
package com.baeldung.functionaldsl.beandeclration
22

3-
import java.math.BigDecimal
43
import org.springframework.boot.autoconfigure.SpringBootApplication
54
import org.springframework.boot.runApplication
65
import org.springframework.context.support.BeanDefinitionDsl
76
import org.springframework.context.support.beans
7+
import java.math.BigDecimal
88

99
@SpringBootApplication
1010
open class Application
@@ -20,9 +20,9 @@ fun main(vararg args: String) {
2020
isPrimary = false,
2121
function = {
2222
BigDecimal(1.0)
23-
}
23+
},
2424
)
25-
}
25+
},
2626
)
2727
}
28-
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
package com.baeldung.functional_dsl.regular_controller
1+
package com.baeldung.functionaldsl.regularcontroller
22

3-
import org.springframework.boot.autoconfigure.SpringBootApplication
4-
import org.springframework.boot.runApplication
53
import org.springframework.web.bind.annotation.GetMapping
64
import org.springframework.web.bind.annotation.PathVariable
75
import org.springframework.web.bind.annotation.RequestHeader
@@ -10,17 +8,16 @@ import org.springframework.web.bind.annotation.RestController
108

119
@RestController
1210
class RegularController {
13-
1411
@GetMapping(path = ["/endpoint/{country}"])
1512
fun getPerson(
1613
@RequestParam name: String,
1714
@RequestHeader(name = "X-age") age: String,
18-
@PathVariable country: String
15+
@PathVariable country: String,
1916
): Map<*, *> {
2017
return mapOf(
2118
"name" to name,
2219
"age" to age,
23-
"country" to country
20+
"country" to country,
2421
)
2522
}
26-
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
package com.baeldung.functional_dsl.router_function
1+
package com.baeldung.functionaldsl.routerfunction
22

33
import org.springframework.boot.autoconfigure.SpringBootApplication
44
import org.springframework.boot.runApplication
55
import org.springframework.context.annotation.Bean
66
import org.springframework.web.servlet.function.RouterFunction
77
import org.springframework.web.servlet.function.RouterFunctions
8-
import org.springframework.web.servlet.function.ServerRequest
98
import org.springframework.web.servlet.function.ServerResponse
109

1110
@SpringBootApplication
1211
open class Application {
13-
1412
@Bean
1513
open fun configure(): RouterFunction<ServerResponse> {
1614
return RouterFunctions.route()
@@ -19,8 +17,8 @@ open class Application {
1917
mapOf(
2018
"name" to it.param("name"),
2119
"age" to it.headers().header("X-age")[0],
22-
"country" to it.pathVariable("country")
23-
)
20+
"country" to it.pathVariable("country"),
21+
),
2422
)
2523
}
2624
.build()
@@ -29,4 +27,4 @@ open class Application {
2927

3028
fun main(vararg args: String) {
3129
runApplication<Application>(*args)
32-
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.functional_dsl.router_function_dsl
1+
package com.baeldung.functionaldsl.routerfunctiondsl
22

33
import org.springframework.boot.autoconfigure.SpringBootApplication
44
import org.springframework.boot.runApplication
@@ -11,8 +11,7 @@ import org.springframework.web.servlet.function.router
1111
open class Application
1212

1313
fun main(vararg args: String) {
14-
runApplication<Application>(*args)
15-
{
14+
runApplication<Application>(*args) {
1615
addInitializers(
1716
beans {
1817
bean {
@@ -22,13 +21,13 @@ fun main(vararg args: String) {
2221
mapOf(
2322
"name" to it.param("name"),
2423
"age" to it.headers().header("X-age")[0],
25-
"country" to it.pathVariable("country")
26-
)
24+
"country" to it.pathVariable("country"),
25+
),
2726
)
2827
}
2928
}
3029
}
31-
}
30+
},
3231
)
3332
}
34-
}
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.functionaldsl.regularcontroller;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4+
5+
import com.baeldung.functionaldsl.regularcontroller.RegularController;
6+
import com.fasterxml.jackson.core.type.TypeReference;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import java.util.Map;
9+
import org.assertj.core.api.Assertions;
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
13+
import org.springframework.test.context.ContextConfiguration;
14+
import org.springframework.test.web.servlet.MockMvc;
15+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
16+
17+
@WebMvcTest(controllers = RegularController.class)
18+
@ContextConfiguration(classes = RegularController.class)
19+
public class RegularControllerTest {
20+
21+
@Autowired
22+
private MockMvc mockMvc;
23+
24+
private ObjectMapper objectMapper = new ObjectMapper();
25+
26+
@Test
27+
void test() throws Exception {
28+
Map<String, Object> result = objectMapper.readValue(mockMvc
29+
.perform(get("/endpoint/anything").header("X-age", 22).param("name", "Mikhail"))
30+
.andExpect(MockMvcResultMatchers.status().is2xxSuccessful())
31+
.andReturn()
32+
.getResponse()
33+
.getContentAsString(), new TypeReference<Map<String, Object>>() {
34+
});
35+
36+
Assertions.assertThat(result).containsKeys("name", "age", "country");
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.functionaldsl.routerfunction;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4+
5+
import com.baeldung.functionaldsl.routerfunction.Application;
6+
import com.fasterxml.jackson.core.type.TypeReference;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import java.util.Map;
9+
import org.assertj.core.api.Assertions;
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
13+
import org.springframework.test.context.ContextConfiguration;
14+
import org.springframework.test.web.servlet.MockMvc;
15+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
16+
17+
@WebMvcTest(controllers = Application.class)
18+
@ContextConfiguration(classes = Application.class)
19+
public class RouterFunctionTest {
20+
21+
@Autowired
22+
private MockMvc mockMvc;
23+
24+
private ObjectMapper objectMapper = new ObjectMapper();
25+
26+
@Test
27+
void test() throws Exception {
28+
Map<String, Object> result = objectMapper.readValue(mockMvc
29+
.perform(get("/endpoint/anything").header("X-age", 22).param("name", "Mikhail"))
30+
.andExpect(MockMvcResultMatchers.status().is2xxSuccessful())
31+
.andReturn()
32+
.getResponse()
33+
.getContentAsString(), new TypeReference<Map<String, Object>>() {
34+
});
35+
36+
Assertions.assertThat(result).containsKeys("name", "age", "country");
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.functionaldsl.routerfunctiondsl;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4+
5+
import com.baeldung.functionaldsl.routerfunctiondsl.Application;
6+
import com.fasterxml.jackson.core.type.TypeReference;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import java.util.Map;
9+
import org.assertj.core.api.Assertions;
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
13+
import org.springframework.test.context.ContextConfiguration;
14+
import org.springframework.test.web.servlet.MockMvc;
15+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
16+
17+
@WebMvcTest(controllers = Application.class)
18+
@ContextConfiguration(classes = Application.class)
19+
public class RouterDslTest {
20+
21+
@Autowired
22+
private MockMvc mockMvc;
23+
24+
private ObjectMapper objectMapper = new ObjectMapper();
25+
26+
@Test
27+
void test() throws Exception {
28+
Map<String, Object> result = objectMapper.readValue(mockMvc
29+
.perform(get("/endpoint/anything").header("X-age", 22).param("name", "Mikhail"))
30+
.andExpect(MockMvcResultMatchers.status().is2xxSuccessful())
31+
.andReturn()
32+
.getResponse()
33+
.getContentAsString(), new TypeReference<Map<String, Object>>() {
34+
});
35+
36+
Assertions.assertThat(result).containsKeys("name", "age", "country");
37+
}
38+
}

0 commit comments

Comments
 (0)