简体   繁体   English

如何在 junit 中为 Jooq Select Query 编写单元测试

[英]How to write Unit testing for Jooq Select Query in junit

 public <Class> getClassbyName(String name) {
     private DSLContext context;
    
     return context.selectFrom(Table)
                   .where(Table.NAME.equal(name))
                   .fetchOneInto(Class.class);
} 

I have this kind of function.我有这种功能。 I need to write a unit test for this Select Query in JOOQ.我需要在 JOOQ 中为这个 Select Query 编写一个单元测试。 does anyone have any idea?有谁有想法吗?

Testing clients of your methods测试您的方法的客户

If you want to test the logic of that method's callers, you could mock the method with a third party library like mockito .如果您想测试该方法调用者的逻辑,您可以使用像mockito这样的第三方库来模拟该方法。 This will allow you to produce a set of expected Class return values for a set of known input String name values.这将允许您为一组已知的输入String name值生成一组预期的Class返回值。

You can also integration test everything as shown below, that works for your entire application.您还可以集成测试如下所示的所有内容,这适用于您的整个应用程序。

You could try to mock jOOQ itself ( and jOOQ offers such tooling ), but I highly recommend against it.您可以尝试模拟 jOOQ 本身( 并且 jOOQ 提供了此类工具),但我强烈建议您不要这样做 Sooner than later, you'll be implementing an entire RDBMS.迟早,您将实现整个 RDBMS。

Testing your query's correctness测试查询的正确性

If you want to be sure that your query itself is correct and doesn't produce eg undesired cartesian products or null values, etc. you should run integration tests.如果您想确保您的查询本身是正确的并且不会产生例如不需要的笛卡尔积或null值等,您应该运行集成测试。

Ideally, your integration tests are as close as possible to your production environment.理想情况下,您的集成测试尽可能接近您的生产环境。 For example, if you're using PostgreSQL, then you should run this query on an actual PostgreSQL instance with a known data set.例如,如果您使用的是 PostgreSQL,那么您应该在具有已知数据集的实际 PostgreSQL 实例上运行此查询。 A good utility for running such tests is testcontainers , but there are also other ways to automate tests against an actual database instance.运行此类测试的一个很好的实用程序是testcontainers ,但还有其他方法可以针对实际数据库实例自动进行测试。

A less recommended way (but faster and maybe more convenient if your queries are simple) would be to run your integration tests on an in-memory database, such as H2 .一种不太推荐的方法(但如果您的查询很简单,可能会更快,也可能更方便)是在内存数据库上运行集成测试,例如H2 This is faster, but the price is high:这更快,但价格高:

  • You can no longer use vendor specific features of your production database product您不能再使用生产数据库产品的供应商特定功能
  • You will have to tweak your database schema to the least common denominator of what's supported between your production database and the test database, eg data types, etc.您必须将数据库架构调整为生产数据库和测试数据库之间支持的最小公分母,例如数据类型等。

However, this is a viable option if your application supports more than one production database product, in case of which the above two caveats are a problem you have in production anyway.但是,如果您的应用程序支持多个生产数据库产品,则这一个可行的选择,在这种情况下,无论如何上述两个警告都是您在生产中遇到的问题。

I'd still use testcontainers for most tests, though.不过,我仍然会在大多数测试中使用 testcontainers。 Here's a quick example how easy it is to set up code generation with testcontainers, for example: https://github.com/jOOQ/jOOQ/tree/main/jOOQ-examples/jOOQ-testcontainers-example这是一个快速示例,说明使用 testcontainers 设置代码生成是多么容易,例如: https : //github.com/jOOQ/jOOQ/tree/main/jOOQ-examples/jOOQ-testcontainers-example

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

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