From b0a78c3aa189cc711938c2515231be2f1ebd14b3 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Fri, 15 Feb 2019 18:16:09 +0100 Subject: [PATCH] Polishing --- README.adoc | 102 +++++++++++++------ build.gradle | 2 +- pom.xml | 4 - src/main/kotlin/blog/BlogConfiguration.kt | 16 +-- src/test/kotlin/blog/HttpControllersTests.kt | 4 +- 5 files changed, 83 insertions(+), 45 deletions(-) diff --git a/README.adoc b/README.adoc index 22245f0..1c9f63a 100644 --- a/README.adoc +++ b/README.adoc @@ -361,7 +361,7 @@ While JUnit 4 is still the default testing framework provided with Spring Boot, === Switching from JUnit 4 to JUnit 5 -Enable JUnit 5 support by adding the following line to your `build.gradle` file: +With Gradle, enable JUnit 5 support by adding the following line to your `build.gradle` file: `build.gradle` [source,groovy] @@ -385,7 +385,28 @@ dependencies { } ---- -Refresh Gradle configuration, open `BlogApplicationTests` and update imports as bellow and remove `@RunWith(SpringRunner::class)` since `@SpringBootTest` is already annotated with JUnit 5 `@ExtendWith(SpringExtension::class)` as of Spring Boot 2.1. +With Maven: +[source,xml] +---- + + org.springframework.boot + spring-boot-starter-test + test + + + junit + junit + + + + + org.junit.jupiter + junit-jupiter-engine + test + +---- + +Refresh the build configuration, open `BlogApplicationTests` and update imports as bellow and remove `@RunWith(SpringRunner::class)` (Spring Boot 2.1+ test annotations are already annotated with JUnit 5 `@ExtendWith(SpringExtension::class)`). `src/test/kotlin/blog/BlogApplicationTests.kt` [source,kotlin] @@ -739,21 +760,23 @@ class BlogConfiguration { val smaldini = userRepository.save(User("smaldini", "Stéphane", "Maldini")) articleRepository.save(Article( - "Reactor Bismuth is out", - "Lorem ipsum", - "dolor sit amet", - smaldini + title = "Reactor Bismuth is out", + headline = "Lorem ipsum", + content = "dolor sit amet", + author = smaldini )) articleRepository.save(Article( - "Reactor Aluminium has landed", - "Lorem ipsum", - "dolor sit amet", - smaldini + title = "Reactor Aluminium has landed", + headline = "Lorem ipsum", + content = "dolor sit amet", + author = smaldini )) } } ---- +NOTE: Notice the usage of named parameters to make the code more readable. + And we also update the integration tests accordingly. `src/test/kotlin/blog/IntegrationTests.kt` @@ -830,7 +853,45 @@ For tests, instead of integration tests, we are going to leverage `@WebMvcTest` Since `@MockBean` and `@SpyBean` annotations are specific to Mockito, we are going to leverage https://github.com/Ninja-Squad/springmockk[SpringMockK] which provides similar `@MockkBean` and `@SpykBean` annotations for Mockk. -`src/test/kotlin/blog/HttpApiTests.kt` +With Gradle: + +[source,groovy] +---- +testCompile('org.springframework.boot:spring-boot-starter-test') { + exclude module: 'junit' + exclude module: 'mockito-core' +} +testImplementation('com.ninja-squad:springmockk:1.1.0') +---- + +Or with Maven: + +[source,xml] +---- + + org.springframework.boot + spring-boot-starter-test + test + + + junit + junit + + + org.mockito + mockito-core + + + + + com.ninja-squad + springmockk + 1.1.0 + test + +---- + +`src/test/kotlin/blog/HttpControllersTests.kt` [source,kotlin] ---- @WebMvcTest @@ -871,25 +932,6 @@ class HttpControllersTests(@Autowired val mockMvc: MockMvc) { } ---- -With Gradle: - -[source,groovy] ----- -testImplementation('com.ninja-squad:springmockk:1.1.0') ----- - -Or with Maven: - -[source,xml] ----- - - com.ninja-squad - springmockk - 1.1.0 - test - ----- - NOTE: `$` needs to be escaped in strings as it is used for string interpolation. == Configuration properties diff --git a/build.gradle b/build.gradle index 49828d3..31883a9 100644 --- a/build.gradle +++ b/build.gradle @@ -64,6 +64,6 @@ dependencies { exclude module: 'mockito-core' } testImplementation('org.junit.jupiter:junit-jupiter-api') - testImplementation('com.ninja-squad:springmockk:1.1.0') testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine') + testImplementation('com.ninja-squad:springmockk:1.1.0') } diff --git a/pom.xml b/pom.xml index 1f2b684..c8c00bc 100644 --- a/pom.xml +++ b/pom.xml @@ -141,10 +141,6 @@ - - maven-surefire-plugin - 2.22.1 - diff --git a/src/main/kotlin/blog/BlogConfiguration.kt b/src/main/kotlin/blog/BlogConfiguration.kt index b164cd5..76f10d4 100644 --- a/src/main/kotlin/blog/BlogConfiguration.kt +++ b/src/main/kotlin/blog/BlogConfiguration.kt @@ -13,16 +13,16 @@ class BlogConfiguration { val smaldini = userRepository.save(User("smaldini", "Stéphane", "Maldini")) articleRepository.save(Article( - "Reactor Bismuth is out", - "Lorem ipsum", - "dolor sit amet", - smaldini + title = "Reactor Bismuth is out", + headline = "Lorem ipsum", + content = "dolor sit amet", + author = smaldini )) articleRepository.save(Article( - "Reactor Aluminium has landed", - "Lorem ipsum", - "dolor sit amet", - smaldini + title = "Reactor Aluminium has landed", + headline = "Lorem ipsum", + content = "dolor sit amet", + author = smaldini )) } } diff --git a/src/test/kotlin/blog/HttpControllersTests.kt b/src/test/kotlin/blog/HttpControllersTests.kt index bb5769f..c189620 100644 --- a/src/test/kotlin/blog/HttpControllersTests.kt +++ b/src/test/kotlin/blog/HttpControllersTests.kt @@ -14,10 +14,10 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.* class HttpControllersTests(@Autowired val mockMvc: MockMvc) { @MockkBean - private lateinit var userRepository: UserRepository + lateinit var userRepository: UserRepository @MockkBean - private lateinit var articleRepository: ArticleRepository + lateinit var articleRepository: ArticleRepository @Test fun `List articles`() {