问题
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