简体   繁体   English

Java Spring Boot集成测试数据库修改和认证

[英]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. 但是,在发出请求之前,我必须向/authenticate端点发出请求以检索令牌,以便可以将其放入请求的标头中。 I have 2 questions 我有两个问题

  1. The username and password I use locally validates against a dev LDAP server. 我在本地使用的用户名和密码可针对dev LDAP服务器进行验证。 As the application gets deployed to higher environments the LDAP endpoint changes to the respective level. 随着应用程序部署到更高的环境,LDAP端点将更改为相应级别。 So in environments higher than DEV the integration test will fail because that username/password combo won't work. 因此,在高于DEV的环境中,集成测试将失败,因为该用户名/密码组合将无法使用。 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? 有没有更好的方法来执行此操作,例如迫使spring完全忽略身份验证?
  2. The integration test tests all the controller's endpoints like /add , /get etc. This unfortunately actually modifies the database (expected). 集成测试测试所有控制器的端点,例如/add/get等。不幸的是,这实际上修改了数据库(预期的)。 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. 1)在单元测试类中,如果要使用@TestPropertySource(properties = {"security.basic.enabled=false"})覆盖您的安全设置,请使用@TestPropertySource(locations="classpath:test.properties")完全不同的属性文件测试。

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: 2)您可以使用模拟,或者如果您不想在集成测试中使用模拟,那么每次测试之后,您都必须使用类似以下的方法来清理数据库:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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