SpringBootTest is connecting to database

眉间皱痕 提交于 2021-01-28 02:43:38

问题


I have a test that is testing parts of a spring application. It is using SpringRunner and the annotaion @SpringBootTest so it is starting a full spring server.

Problem is that the test is being executed by a server that does not have access to the database, so I am getting a lot of connection timeouts that is slowing down the test.

The connection issues in itself isn't really a problem as the tests are mocking the calls to the database, and so they are not relying on the connection being there. It is just that the tests are slow(and ugly) with it.

So the tests looks kind of like this:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class DispatcherTest

and it uses this property file

spring.datasource.url:jdbc:oracle:thin:@100.32.13.32:1521:TEST
spring.datasource.username:sa
spring.datasource.password:password
spring.datasource.driver-class-name:oracle.jdbc.OracleDriver
spring.jpa.database-platform:org.hibernate.dialect.Oracle10gDialect

I think that the issue is that there are a lot of different JPA repositories that are being scanned like this

@EnableJpaRepositories("package.*")

So is there any fancy way of telling spring not to connect to the database or do I have to mock every single JPA repository class?


回答1:


You can use spring profiles to split your configuration. Something like this:

In this scenario, I have three profiles to split each configuration. You can see the docs here and here.

To start an application with some profiles, just do this:

java -Dspring.profiles.active=development -jar yourApplication.jar

In your case, you can use a profile test to connect an embedded (such as H2) or another local database for testing impls.

(I'm not a native English speaker, may contain grammar errors)



来源:https://stackoverflow.com/questions/53169935/springboottest-is-connecting-to-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!