Polishing

This commit is contained in:
Sebastien Deleuze
2019-02-15 18:16:09 +01:00
parent 98d418e2e8
commit b0a78c3aa1
5 changed files with 83 additions and 45 deletions

View File

@@ -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]
----
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
----
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]
----
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.ninja-squad</groupId>
<artifactId>springmockk</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
----
`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]
----
<dependency>
<groupId>com.ninja-squad</groupId>
<artifactId>springmockk</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
----
NOTE: `$` needs to be escaped in strings as it is used for string interpolation.
== Configuration properties

View File

@@ -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')
}

View File

@@ -141,10 +141,6 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</build>

View File

@@ -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
))
}
}

View File

@@ -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`() {