简体   繁体   English

使用可放心的弹簧引导集成测试

[英]spring-boot integration testing using rest-assured

I have been struggling with the issue of starting the H2 db in my tests. 我一直在努力在测试中启动H2 db的问题。 This is how the tests are annotated - 这就是测试的注释方式-

  @SpringApplicationConfiguration(classes = ServiceApplication.class)
@WebAppConfiguration
@IntegrationTest("server.port:8084")
@PropertySource("test:application.properties")
public class testSerivce{
//test scenario
}

The in memory db H2 is used and this is how its setup in application property file - 使用内存db H2,这是它在应用程序属性文件中的设置方式-

dataSource.driverClassName=org.h2.Driver
dataSource.url=jdbc:h2:tcp://localhost:59000/./target/service-db;MODE=Oracle;IGNORECASE=TRUE;IFEXISTS=TRUE
dataSource.username=sa
dataSource.password=sa
dataSource.maxActive=2
dataSource.initialSize=1
dataSource.maxIdle=1

When I right click and run the tests the H2 db is not getting setup with the service and the tests are not able to connect with database and failing. 当我右键单击并运行测试时,H2 db无法通过该服务进行设置,并且测试无法连接数据库并失败。

Whereas when I run the tests using the maven profile,as thy are running in teamcity everything works and the tests work but it runs all the 10tests . 而当我使用Maven配置文件运行测试时,就像您在teamcity中运行一样,一切正常,并且测试正常,但是它运行了所有10tests。

What should I do? 我该怎么办? I am stuck at this issue for many days. 我在这个问题上停留了很多天。 Which annotation should I use or how to setup the H2 setup so that they also run with the service. 我应该使用哪个注释或如何设置H2设置,以便它们也可以与服务一起运行。

Are you using Eclipse? 您正在使用Eclipse吗? I somehow noticed that src/main/resources/application.properties gets excluded from the Java Build Path, if you generate the project with the "eclipse:eclipse" maven goal. 我以某种方式注意到,如果生成具有“ eclipse:eclipse” maven目标的项目,则src / main / resources / application.properties将从Java Build Path中排除。

To workaround this you can either add a src/test/resources/application.properties or correct the Java Build Path. 要解决此问题,您可以添加src / test / resources / application.properties或更正Java Build Path。

I don't know if this behavior is intended. 我不知道这种行为是否是故意的。 I guess it is because of the resource exclusions/inclusions in spring-boot-starter-parent 我猜这是由于spring-boot-starter-parent中的资源排除/包含

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

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