MockDataProvider with complex records

361 views
Skip to first unread message

gui...@gmail.com

unread,
Mar 19, 2014, 6:02:37 PM3/19/14
to jooq...@googlegroups.com
Hi !

I'm trying to setup unit tests with jOOQ using MockDataProvider and MockConnection.
I'm having issues setting up a simple unit test where my query does not return a simple Record.

The documentation http://www.jooq.org/doc/3.2/manual/tools/jdbc-mocking/ uses the following:

            Result<AuthorRecord> result = create.newResult(AUTHOR);
            result.add(create.newRecord(AUTHOR));
            result.get(0).setValue(AUTHOR.ID, 1);
            result.get(0).setValue(AUTHOR.LAST_NAME, "Orwell");
            mock[0] = new MockResult(1, result); 
 
However, if my jOOQ query is a simple 

create.select(DSL.count()).from(AUTHOR))

How should I mock the result? I cannot use create.newResult(?)since the count isn't a Field in any Record.
I haven't found a simple way to create a MockResult instance based on a bunch of fields that are not part of a Record object.
Are there any examples of this online?

Thanks !
Guillaume

Lukas Eder

unread,
Mar 20, 2014, 4:37:15 AM3/20/14
to jooq...@googlegroups.com
Hi Guillaume,

Thanks for pointing this out. You're right, this is missing. There should be overloaded methods like:

<T1> Result<Record1<T1>>     newResult(Field<T1> field1);
<T2> Result<Record2<T1, T2>> newResult(Field<T1> field1, Field<T2> field2);

Similar methods exist for constructing records, but not for constructing Results. I have registered issue #3139 for this:

As a workaround, you can patch jOOQ to construct such Result objects yourself. If you look at the existing newResult() method, it only does:

    @Override
    public <R extends Record> Result<R> newResult(Table<R> table) {
        return new ResultImpl<R>(configuration, table.fields());
    }

So you could make the ResultImpl accessible through reflection, and pass your own set of fields to the constructor.

Hope this helps,
Lukas

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Guillaume Simard

unread,
Mar 20, 2014, 9:34:07 AM3/20/14
to jooq...@googlegroups.com
Thanks for the quick answer Lukas,

I have decided to copy ResultImpl into my test classes and expose its default constructor.
I can then easily create a new ResultImpl(new DefaultConfiguration(), myListOfFields);
With a little abstraction layer I think my unit tests will be pretty clean.

Cheers!

Lukas Eder

unread,
Mar 25, 2014, 12:28:47 PM3/25/14
to jooq...@googlegroups.com
Hi Guillaume,

#3139 is now implemented on GitHub master and will be included in jOOQ 3.4. 

Thanks again for reporting,
Lukas

Raman Gupta

unread,
May 12, 2014, 10:45:07 AM5/12/14
to jooq...@googlegroups.com
FYI, until jOOQ 3.4 comes out, the reflection method worked perfectly for me. Here is my Scala implementation I added in to my database unit test base trait:

  def newResult[R <: Record](configuration: Configuration, fields: Array[Field[_]]): Result[R] = {
    val constructor = Class.forName("org.jooq.impl.ResultImpl")
      .getDeclaredConstructor(classOf[Configuration], classOf[Array[Field[_]]])
    constructor.setAccessible(true)
    constructor.newInstance(configuration, fields).asInstanceOf[Result[R]]
  }

Regards,
Raman



On Thursday, March 20, 2014 4:37:15 AM UTC-4, Lukas Eder wrote:

Lukas Eder

unread,
May 12, 2014, 11:05:48 AM5/12/14
to jooq...@googlegroups.com
Nice workaround, thanks for your contribution!

Lukas
Reply all
Reply to author
Forward
0 new messages