[英]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 我有两个问题
/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.