Home > Enterprise >  How to create a context in Spring Boot Test and MockMvc?
How to create a context in Spring Boot Test and MockMvc?

Time:08-26

By example, have a simple login and logout application in Java Spring Boot 3, and i need create the test class, in my project has tree .java files, AuthLoginController.java, AuthLoginOAUTHController.java and AuthLogoutController.java, in test package i create two files, the login controller and logout controller, the login controller return a JWT. The problem is when need test logout controller, need the JWT for call the logout but the login only can call from login controller, can not call login and logout in same class, the test unit 5 require use same class name and same function name, but need correlationate two functions in a unique flow (login and logout).

The problem is similar for others controllers. What is the best practice for create the test?, create java files without controller name and call individual request for each? or create a super global map as application context setting and set/get values for each function forcing the order in each test?

CodePudding user response:

It looks like a business or functional test from the description. So, the test case must not be specific to a controller class.

If you want to test both Login and Logout in a test case, then the test case should only deal with the URLs of login and Logout. Not controller classes themselves.

Another option is to auto-wire both login and logout controllers.

Check out the guide here. Test Web MVC

  • Related