我正在尝试添加测试来测试控制器,但模拟依赖项。
@MicronautTest
class PostControllerTest(private val posts: PostRepository, @Client("/") private val client: HttpClient) : StringSpec({
"test get posts endpoint" {
every { posts.findAll() }
.returns(
listOf(
Post(
id = UUID.randomUUID(),
title = "test title",
content = "test content",
status = Status.DRAFT,
createdAt = LocalDateTime.now()
)
)
)
val request = HttpRequest.GET<Any>("/posts")
val bodyType = Argument.listOf(Post::class.java).type
val response = client.toBlocking().exchange(request, bodyType)
response.status shouldBe HttpStatus.OK
response.body()!![0].title shouldBe "test title"
verify(exactly = 1) { posts.findAll() }
}
@MockBean(PostRepository::class)
fun posts() = mockk<PostRepository>()
})
这不起作用,由于PostRepsoitory
无法识别嘲笑。
运行测试时更改为以下内容。
@MicronautTest
class PostControllerTest(
private val postsBean: PostRepository,
@Client("/") private var client: HttpClient
) : FunSpec({
test("test get posts endpoint") {
val posts = getMock(postsBean)
every { posts.findAll() }
.returns(
listOf(
Post(
id = UUID.randomUUID(),
title = "test title",
content = "test content",
status = Status.DRAFT,
createdAt = LocalDateTime.now()
)
)
)
val request = HttpRequest.GET<Any>("/posts")
val bodyType = Argument.listOf(Post::class.java).type
val response = client.toBlocking().exchange(request, bodyType)
response.status shouldBe HttpStatus.OK
response.body()!![0].title shouldBe "test title"
verify(exactly = 1) { posts.findAll() }
}
}) {
@MockBean(PostRepository::class)
fun posts() = mockk<PostRepository>()
}
并得到了这样的例外。
io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type [com.example.PostControllerTest]
Message: Retrieving the port from the server before it has started is not supported when binding to a random port
Path Taken: new DataInitializer(PostRepository posts) --> new DataInitializer([PostRepository posts]) --> new PostControllerTest(PostRepository postsBean,[HttpClient client])
DataInitializer
用于监听和StartupEvent
插入样本数据。如何在运行测试之前确保应用程序成功启动。
完整的代码在这里。
还有另一个用 Junit5 和 Mockito 编写的模拟示例,效果很好。