Using the MockDataProvider from JOOQ, how do I set the returned lastId ()?

Note. I do not use the Jooq code generator

In the DAO unit test, the DAO checks that after the object has been inserted, the DAO sets the identifier returned by last_insert_id () from the database. Not related to the actual database, since I use MockConnection and MockDataProvider from JOOQ.

When the DAO does the following:

DSLContext ctx = DSL.using(connection, SQLDialect.MYSQL);
//insert
//get id
BigInteger id = ctx.lastId();

JOOQ executes the following query:

select last_insert_id() from dual;

In my MockDataProvider, I check when this request is executed, and return the result accordingly:

import static org.jooq.impl.DSL.dual;

//other code

@Override
public MockResult[] execute(MockExecuteContext ctx) throws SQLException {
   Field<BigInteger> id = DSL.field("", BigInteger.class);
   Record record = dual().newRecord();
   record.setValue(id, BigInteger.valueOf(1234567));
   return new MockResult[] { new MockResult(record) };             
}

When the MockResult returned above returns, I get the following exception

java.lang.IllegalArgumentException: Field () is not contained in Row ()

What is the correct way to populate MockResult for request last_insert_id ()?

+4
1

MockDataProvider DSLContext.lastID():

BigInteger expected = BigInteger.valueOf(1234567);
DSLContext ctx = DSL.using(new MockConnection(c -> {
    Field<BigInteger> id = DSL.field("last_insert_id()", BigInteger.class);
    Record record = DSL.using(MYSQL).newRecord(id);
    record.setValue(id, expected);
    return new MockResult[] { new MockResult(record) };
}), SQLDialect.MYSQL);

assertEquals(expected, ctx.lastID());

:

last_insert_id() ( , jOOQ 3.5.3), :

    Field<BigInteger> id = DSL.field("last_insert_id()", BigInteger.class);

, . dual() . , jOOQ from dual, , Record, last_insert_id():

    Record record = DSL.using(MYSQL).newRecord(id);

, jOOQ, 3.5.3. , , DSLContext.lastID(),

select last_insert_id() from dual
+3

All Articles