How to call the login of spring security when java does automated testing

for example, the project is required to do automated test cases, and I have found some materials to see what has been done, but now there is a problem, that is, in some of my methods, I need to obtain the current user, and the permission part is entrusted to spring security to do. How should I take this user

think of three ideas: 1 is to be a simulated user, 2 is @ Before to call the login method, 3 is refactoring code

1 did not make any progress for a long time in the afternoon, 3 is a little stupid and the cost is too high, 2, then what should I do? There has been some progress in invoking the login request directly with mockmvc, but I find that the call seems to be to visit the page rather than submit the form. Because the printing in the loadUserByUsername method I copied never appeared

ask the great gods for advice. Both methods are fine, and it"s best to say both.

the project is spring boot + spring security and other things should have nothing to do with this problem


you can try multi-context switching using Spring boot. Add security.basic.enable=false and management.security.enabled=false to the configuration file of the test environment to turn off Spring Security. Add @ ActiveProfiles to the test case to switch to the test environment.

Menu