LiranBo
LiranBo

Reputation: 2136

JOOQ mock result with mocked fields

I'm writing UT's for a project that uses JOOQ library to query over SQL, and I need to mock the SQL response.

I've tried following this manual, however, we use JOOQ with pure SQL, meaning we don't have any predefined classes for Fields or Tables.

My question is - how can I define the Result object, without providing a Field as parameter? or defining a mock field param?

        Result<Record2<Integer, String>> result = create.newResult(AUTHOR.ID, AUTHOR.LAST_NAME);    // replace the AUTHOR.ID and AUTHOR.LAST_NAME with some string for example
        result.add(create.newRecord(AUTHOR.ID, AUTHOR.LAST_NAME));
        result.get(0).setValue(AUTHOR.ID, 1);
        result.get(0).setValue(AUTHOR.LAST_NAME, "Orwell");
        mock[0] = new MockResult(1, result);

I noticed that I can define a new field, but the interfaces that Field implements are quite extensive, so I'm looking for something simpler.

Thanks!

Upvotes: 3

Views: 2531

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 221115

Whenever you're using jOOQ without the code generator, you will want to construct Table and Field references dynamically using the plain SQL API. To quote your example:

// Dynamic field creation
Field<Integer> id = field(name("AUTHOR", "ID"), SQLDataType.INTEGER);
Field<String> lastName = field(name("AUTHOR", "LAST_NAME"), SQLDataType.VARCHAR);

// Same as before
Result<Record2<Integer, String>> result = create.newResult(id, lastName);
result.add(create.newRecord(id, lastName).values(1, "Orwell"));
mock[0] = new MockResult(1, result);

See the following sections of the manual for details:

Upvotes: 4

Related Questions