简体   繁体   中英

Java Spring Boot Integration Tests Database Modification and Authentication

I have an integration test that tests a controller. I pass in a request and get a response back and the test passes. I however, before making the request, have to make a request to the /authenticate endpoint to retrieve a token so I can put it in the header of the request. I have 2 questions

  1. The username and password I use locally validates against a dev LDAP server. As the application gets deployed to higher environments the LDAP endpoint changes to the respective level. So in environments higher than DEV the integration test will fail because that username/password combo won't work. So what is the best way to source the username/password to match the environment? Is there a better way to do this like forcing spring to forget about authentication entirely?
  2. The integration test tests all the controller's endpoints like /add , /get etc. This unfortunately actually modifies the database (expected). Is there any way to run these tests but not actually modify the database?

1) In your Unit test class, Use @TestPropertySource(properties = {"security.basic.enabled=false"}) to override your security setting or @TestPropertySource(locations="classpath:test.properties") if you want to use a totally different properties file testing.

2) You could use mocks, or if you don't want to use mocks in your integration tests, then you will have to clean the database after each test using something like this:

@After      
public void tearDown() {
     cleanupDatabase();
 }

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