课程: Complete Guide To Java Testing with JUnit 5 & Mockito

今天就学习课程吧!

今天就开通帐号,24,600 门业界名师课程任您挑!

Test Spring controllers

Test Spring controllers

- [Instructor] Let's finish up our testing for this user application by adding a test class for the controller. Like with our hello controller, we'll annotate this class with WebMvcTest and we'll pass in our UserController class. We'll also autowire the MockMvc. The UserController has the user service as a dependency, so we'll use Mockito to mock its interactions. We use the MockBean annotation here instead of Mock because we want to replace the user service within the Spring application context. We want it to be this mock. Within this test class, we'll also create a user field that we can use in several tests. It'll be our John Doe user. Now let's write a test for the createUser endpoint. When we create a user, we'll be interacting with the saveUser method on the User service, so let's stub that method. Then we'll make a post request on the MockMvc object. We'll also want to send a request body, so let's add that. It'll be in JSON format. The content will be the details of the user…

内容