简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM