[英]Jersey e2e integration tests for in-memory database using Spring's JDBCTemplate
What is the proper way of designing and running e2e integration tests for Jersey REST API that uses jersey-test-framework-provider-inmemory
, h2 database and org.springframework.jdbc.core.JdbcTemplate
? 设计和运行使用
jersey-test-framework-provider-inmemory
,h2数据库和org.springframework.jdbc.core.JdbcTemplate
Jersey REST API的e2e集成测试的正确方法是什么?
Requirements: 要求:
The test scope should be end-to-end: starting from resource and going through all application to the h2 database . 测试范围应该是端到端的:从资源开始并通过所有应用程序到h2数据库 。
Writing tests: 写作测试:
Currently my JUnit integration tests fail if run all together from IDE's JUnit, mainly because they interfere with each other(running concurrently with JUnit). 目前,如果从IDE的JUnit中一起运行,我的JUnit集成测试将失败,主要是因为它们相互干扰(与JUnit并发运行)。 Another issue is those should be rolled back after each test, use transaction support ( currently
@Transactional
annotation does nothing to help). 另一个问题是那些应该在每次测试后回滚,使用事务支持(目前
@Transactional
注释没有任何帮助)。 What's the minimal set of Spring's tools required to support this kind of tests? 支持这种测试所需的最小Spring工具集是什么? How to make it work?
如何使它工作? Should
@Transactional
be placed anywhere else? @Transactional
应该放在其他地方吗?
Example: 例:
@Transactional
public class OrderResourceIT extends JerseyTest {
@Override
protected Application configure() {
// config
}
@Override
protected void configureClient(final ClientConfig config) {
// config
}
@Before
public void startUp(){
// database bootstrap programmatically before each test
}
@Test
@Transactional
public void testGetOrders(){
Response response = target("/orders")
.request()
.get();
List<Order> orders = response.readEntity(new GenericType<List<Order>>(){});
// asserts
}
}
Execution: 执行:
It's planned to execute with maven-failsafe-plugin
: 它计划用
maven-failsafe-plugin
执行:
Failsafe Plugin documentation suggests binding starting of container to pre-integration-test phase
and post-integration-test
at the container termination. Failsafe插件文档建议在容器终止时将容器绑定到
pre-integration-test phase
和post-integration-test
pre-integration-test phase
。 How can it be configured if my jersey-container-grizzly2-http
container is configured programmatically? 如果我的
jersey-container-grizzly2-http
容器是以编程方式配置的,如何配置? Example : 示例 :
public class Main {
// Base URI the Grizzly HTTP server will listen on
public static final String BASE_URI = "http://localhost:8080/myapp/";
public static HttpServer startServer() {
// create a resource config that scans for JAX-RS resources and providers
// in com.example.rest package
final ResourceConfig rc = new ResourceConfig().packages("com.example.rest");
// create and start a new instance of grizzly http server
// exposing the Jersey application at BASE_URI
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}
public static void main(String[] args) throws IOException {
final HttpServer server = startServer();
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
System.in.read();
server.stop();
}
}
Would be perfect to see working example code. 很高兴看到工作示例代码。
I think your scenario is a great fit for dbunit testing. 我认为您的场景非常适合dbunit测试。 With dbunits, an in-memory database is spawned up for every test case to be executed and is destroyed as the test case execution is complete.
使用dbunits,每个要执行的测试用例都会生成一个内存数据库,并在测试用例执行完成时销毁。 The data model of this database entirely depends on the dataset xml you configure.
此数据库的数据模型完全取决于您配置的数据集xml。 Have a look at this: http://archive.oreilly.com/pub/post/dbunit_made_easy.html
看看这个: http : //archive.oreilly.com/pub/post/dbunit_made_easy.html
Hi there are ample of example provided by jersey as jersey-examples . 嗨,泽西提供了充足的例子作为球衣示例。 There are set of junit testcases for every module hosting the rest services.
托管其余服务的每个模块都有一组junit测试用例。
The example can downloaded from jersey download page . 该示例可以从泽西下载页面下载。 https://jersey.java.net/download.html
https://jersey.java.net/download.html
Exact links are given below which may change in future. 下面给出了确切的链接,将来可能会有所改变。
For all available version: 对于所有可用版本:
http://repo1.maven.org/maven2/org/glassfish/jersey/bundles/jersey-examples http://repo1.maven.org/maven2/org/glassfish/jersey/bundles/jersey-examples
For current latest version: 目前的最新版本:
http://repo1.maven.org/maven2/org/glassfish/jersey/bundles/jersey-examples/2.23.1/jersey-examples-2.23.1-all.zip http://repo1.maven.org/maven2/org/glassfish/jersey/bundles/jersey-examples/2.23.1/jersey-examples-2.23.1-all.zip
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.