gpt4 book ai didi

java - 使用 Spring 的 JDBCTemplate 对内存数据库进行 Jersey e2e 集成测试

转载 作者:IT老高 更新时间:2023-10-28 13:57:55 27 4
gpt4 key购买 nike

为使用 jersey-test-framework-provider-inmemory、h2 数据库和 org.springframework.jdbc 的 Jersey REST API 设计和运行 e2e 集成测试的正确方法是什么。 core.JdbcTemplate?

要求:

测试范围应该是端到端的:从resource开始,通过所有应用程序到h2数据库

写作测试:

目前,如果从 IDE 的 JUnit 一起运行,我的 JUnit 集成测试会失败,主要是因为它们相互干扰(与 JUnit 同时运行)。另一个问题是那些应该在每次测试后回滚,使用事务支持(目前 @Transactional 注释无济于事)。支持此类测试所需的最少 Spring 工具集是什么?如何让它发挥作用? @Transactional 应该放在其他任何地方吗?

例子:

@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
}
}

执行:

计划用 maven-failsafe-plugin 执行:

Failsafe Plugin documentation建议在容器终止时将容器的启动绑定(bind)到 pre-integration-test phasepost-integration-test。如果我的 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();
}
}

如果能看到工作示例代码就完美了。

最佳答案

我认为您的方案非常适合 dbunit 测试。使用 dbunits,为每个要执行的测试用例生成一个内存数据库,并在测试用例执行完成时销毁。这个数据库的数据模型完全取决于你配置的数据集xml。看看这个:http://archive.oreilly.com/pub/post/dbunit_made_easy.html

关于java - 使用 Spring 的 JDBCTemplate 对内存数据库进行 Jersey e2e 集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36961497/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com