Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to Unit Test Spring Boot App with Spring Security

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a simple application that I have setup with spring security using a custom MySql Database. You can check out the complete app on github. Now the problem is I'm writing test cases for it and they seems to fail on login page and anything that works after the login. My question is how do I write test cases for it to check the successful login and the subsequent requests?

My Security Config:


My Custom Success Handler:



After some seraching I tried to write test cases like this but they don't seem to be working:



I searched on the net and there are links WithMockUser and UserDetails, but the problem is as you can see I'm setting a my primary key userId in the session in my custom success handler. So I would also need to get the session in my test. Please tell me the simplest way to write tests that will work, possibly with code since I'm new with security and all such.

I had this problem a while back but I haven't found any solutions. Now I'm having it again.
 
Sheriff
Posts: 15761
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMO, the kind testing you're trying to do should not be done at the unit test level. I cringe when I see test code like this because it's difficult to discern right away what you're actually trying to test. All that setup code drowns out the intent of the test. I would test from the browser level using a testing framework like Selenium, and test against a running application. These kind of tests are too slow to be categorized as unit tests anyway. They are really integration tests.
 
Zee Haider
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I won't deny that I'm a newbie to the world of unit testing. So, anyway what would you suggest I should do? And let's assume I do want to test things like that, how would I achieve that?
 
Junilu Lacar
Sheriff
Posts: 15761
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, these are integration tests you're talking about, so you might want to look for tools that allow you to do that. Selenium WebDriver, for example, is a popular tool.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic