Mocking JDBC using jOOQ without generated tables

1,005 views
Skip to first unread message

kevin.we...@gmail.com

unread,
Aug 22, 2013, 10:29:53 AM8/22/13
to jooq...@googlegroups.com
Hello,

I'm using jOOQ version 3.1.0 for SQL building and JDBC for SQL execution. In an attempt to test my application I would like to mock the database as described in the manual: http://www.jooq.org/doc/3.0/manual/tools/jdbc-mocking/. Since I haven't used the code generation I can't use exactly the code as described in the manual. My primitive workaround was the following:

DSLContext create = DSL.using(SQLDialect.POSTGRES);
Table<Record> table = DSL.table("some_table");
Record record = create.newRecord(table);
record.setValue(DSL.field("id"), 1);
Result<Record> result = create.newResult(table);
result.add(record);

However, this code would blow up with an ArrayIndexOutOfBoundsException at org.jooq.impl.AbstractRecord.getValue0(AbstractRecord.java:271). Is there any way of using the mocking features without code generation?

Off topic: the exception was thrown when calling this function of AbstractRecord:
  final <T> Value<T> getValue0(Field<T> field) {
     return getValue0(fieldsRow().indexOf(field));
  }
The method indexOf(field) returns -1 as the field is not found and then calls the overloaded getValue0(int index) method, which contains the following input checking
  if (index >= v.length) {
    throw new IllegalArgumentException("Field " + index + " is not contained in list");
   }
Maybe the comparison could be changed to check also for values smaller than 0.

Thanks a lot!

cheers,
kevin

Lukas Eder

unread,
Aug 23, 2013, 7:26:01 AM8/23/13
to jooq...@googlegroups.com
Hello,

Hello,

I'm using jOOQ version 3.1.0 for SQL building and JDBC for SQL execution. In an attempt to test my application I would like to mock the database as described in the manual: http://www.jooq.org/doc/3.0/manual/tools/jdbc-mocking/. Since I haven't used the code generation I can't use exactly the code as described in the manual. My primitive workaround was the following:

DSLContext create = DSL.using(SQLDialect.POSTGRES);
Table<Record> table = DSL.table("some_table");
Record record = create.newRecord(table);
record.setValue(DSL.field("id"), 1);
Result<Record> result = create.newResult(table);
result.add(record);

However, this code would blow up with an ArrayIndexOutOfBoundsException at org.jooq.impl.AbstractRecord.getValue0(AbstractRecord.java:271). Is there any way of using the mocking features without code generation?

True, this isn't very convenient for users not using the code generator. Your best bet right now is to use CustomTables:

In a CustomTable, you can explicitly declare fields for each table. This area is not yet very sophisticated, from a usability perspective, I'm afraid.
 
Off topic: the exception was thrown when calling this function of AbstractRecord:
  final <T> Value<T> getValue0(Field<T> field) {
     return getValue0(fieldsRow().indexOf(field));
  }
The method indexOf(field) returns -1 as the field is not found and then calls the overloaded getValue0(int index) method, which contains the following input checking
  if (index >= v.length) {
    throw new IllegalArgumentException("Field " + index + " is not contained in list");
   }
Maybe the comparison could be changed to check also for values smaller than 0.

There's a pending fix for this issue, here:

kevin.we...@gmail.com

unread,
Aug 27, 2013, 5:41:06 PM8/27/13
to jooq...@googlegroups.com
Hi,

thanks a lot for your quick help and sorry for my late reply :-)
Before you pointed me to the CustomTable I already made the following dirty hack solve the problem.
However, the CustomTable approach is probably more appropriate than this.

public class DummyTable extends TableImpl<Record> {

public DummyTable(String name) {
super(name);
}

public void addFields(String... fieldNames) {
for (String fieldName : fieldNames)
createField(fieldName, SQLDataType.OTHER, this);
}

}

cheers,
kevin

p.s. sorry for not checking the issue tracker on github first for the ArrayIndexOutOfBoundsException.

Lukas Eder

unread,
Aug 28, 2013, 4:23:52 AM8/28/13
to jooq...@googlegroups.com
Hello,

Hi,

thanks a lot for your quick help and sorry for my late reply :-)
Before you pointed me to the CustomTable I already made the following dirty hack solve the problem.
However, the CustomTable approach is probably more appropriate than this.

Both solutions will work equally well. Just note that CustomTable's API evolution will follow semantic versioning, whereas TableImpl has a disclaimer in the Javadoc, saying that it is for "internal" use only (e.g. for the code generator output)
 
p.s. sorry for not checking the issue tracker on github first for the ArrayIndexOutOfBoundsException.

No worries. I think you couldn't have found it anyway, as it was labeled with "java.lang.ArrayIndexOutOfBoundsException" at the time. GitHub doesn't perform these kinds of partial matches in search...

Cheers
Lukas

Lukas Eder

unread,
Dec 13, 2013, 8:22:40 AM12/13/13
to jooq...@googlegroups.com
#2655 is finally fixed in jOOQ 3.3 and will be merged to jOOQ 3.2.2

Cheers
Lukas


2013/8/28 Lukas Eder <lukas...@gmail.com>
Reply all
Reply to author
Forward
0 new messages