Polishing
This commit is contained in:
102
README.adoc
102
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
|
=== 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`
|
`build.gradle`
|
||||||
[source,groovy]
|
[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`
|
`src/test/kotlin/blog/BlogApplicationTests.kt`
|
||||||
[source,kotlin]
|
[source,kotlin]
|
||||||
@@ -739,21 +760,23 @@ class BlogConfiguration {
|
|||||||
|
|
||||||
val smaldini = userRepository.save(User("smaldini", "Stéphane", "Maldini"))
|
val smaldini = userRepository.save(User("smaldini", "Stéphane", "Maldini"))
|
||||||
articleRepository.save(Article(
|
articleRepository.save(Article(
|
||||||
"Reactor Bismuth is out",
|
title = "Reactor Bismuth is out",
|
||||||
"Lorem ipsum",
|
headline = "Lorem ipsum",
|
||||||
"dolor sit amet",
|
content = "dolor sit amet",
|
||||||
smaldini
|
author = smaldini
|
||||||
))
|
))
|
||||||
articleRepository.save(Article(
|
articleRepository.save(Article(
|
||||||
"Reactor Aluminium has landed",
|
title = "Reactor Aluminium has landed",
|
||||||
"Lorem ipsum",
|
headline = "Lorem ipsum",
|
||||||
"dolor sit amet",
|
content = "dolor sit amet",
|
||||||
smaldini
|
author = smaldini
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
|
NOTE: Notice the usage of named parameters to make the code more readable.
|
||||||
|
|
||||||
And we also update the integration tests accordingly.
|
And we also update the integration tests accordingly.
|
||||||
|
|
||||||
`src/test/kotlin/blog/IntegrationTests.kt`
|
`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.
|
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]
|
[source,kotlin]
|
||||||
----
|
----
|
||||||
@WebMvcTest
|
@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.
|
NOTE: `$` needs to be escaped in strings as it is used for string interpolation.
|
||||||
|
|
||||||
== Configuration properties
|
== Configuration properties
|
||||||
|
|||||||
@@ -64,6 +64,6 @@ dependencies {
|
|||||||
exclude module: 'mockito-core'
|
exclude module: 'mockito-core'
|
||||||
}
|
}
|
||||||
testImplementation('org.junit.jupiter:junit-jupiter-api')
|
testImplementation('org.junit.jupiter:junit-jupiter-api')
|
||||||
testImplementation('com.ninja-squad:springmockk:1.1.0')
|
|
||||||
testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine')
|
testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine')
|
||||||
|
testImplementation('com.ninja-squad:springmockk:1.1.0')
|
||||||
}
|
}
|
||||||
|
|||||||
4
pom.xml
4
pom.xml
@@ -141,10 +141,6 @@
|
|||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
|
||||||
<version>2.22.1</version>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|||||||
@@ -13,16 +13,16 @@ class BlogConfiguration {
|
|||||||
|
|
||||||
val smaldini = userRepository.save(User("smaldini", "Stéphane", "Maldini"))
|
val smaldini = userRepository.save(User("smaldini", "Stéphane", "Maldini"))
|
||||||
articleRepository.save(Article(
|
articleRepository.save(Article(
|
||||||
"Reactor Bismuth is out",
|
title = "Reactor Bismuth is out",
|
||||||
"Lorem ipsum",
|
headline = "Lorem ipsum",
|
||||||
"dolor sit amet",
|
content = "dolor sit amet",
|
||||||
smaldini
|
author = smaldini
|
||||||
))
|
))
|
||||||
articleRepository.save(Article(
|
articleRepository.save(Article(
|
||||||
"Reactor Aluminium has landed",
|
title = "Reactor Aluminium has landed",
|
||||||
"Lorem ipsum",
|
headline = "Lorem ipsum",
|
||||||
"dolor sit amet",
|
content = "dolor sit amet",
|
||||||
smaldini
|
author = smaldini
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,10 +14,10 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
|
|||||||
class HttpControllersTests(@Autowired val mockMvc: MockMvc) {
|
class HttpControllersTests(@Autowired val mockMvc: MockMvc) {
|
||||||
|
|
||||||
@MockkBean
|
@MockkBean
|
||||||
private lateinit var userRepository: UserRepository
|
lateinit var userRepository: UserRepository
|
||||||
|
|
||||||
@MockkBean
|
@MockkBean
|
||||||
private lateinit var articleRepository: ArticleRepository
|
lateinit var articleRepository: ArticleRepository
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `List articles`() {
|
fun `List articles`() {
|
||||||
|
|||||||
Reference in New Issue
Block a user