Quantcast

How to unit test a Domain class with hibernate events

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

How to unit test a Domain class with hibernate events

Dean Del Ponte-2
I have a domain class with a service and hibernate events.

The hibernate events call the service.

When unit testing, the service is always null.

How do I either set the service on the domain class or mock it out?

Example:

class MyDomain {
     def service

     afterInsert() {
          service.doSomething()
     }
}

I'm using Spock for testing and have tried:
Mock out the afterInsert method with:
     MyDomain.metaClass.afterInsert{}  <-  This does nothing.

All help is appreciate.

Thanks!

- Dean Del Ponte
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to unit test a Domain class with hibernate events

longwa
Can't you just create an instance of the service and set it manually on the domain object?

def obj = new MyDomain()
obj.service = new MockWhateverService()
obj.save()

-Aaron

On Thu, Jun 21, 2012 at 10:50 AM, Dean Del Ponte <[hidden email]> wrote:
I have a domain class with a service and hibernate events.

The hibernate events call the service.

When unit testing, the service is always null.

How do I either set the service on the domain class or mock it out?

Example:

class MyDomain {
     def service

     afterInsert() {
          service.doSomething()
     }
}

I'm using Spock for testing and have tried:
Mock out the afterInsert method with:
     MyDomain.metaClass.afterInsert{}  <-  This does nothing.

All help is appreciate.

Thanks!

- Dean Del Ponte

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to unit test a Domain class with hibernate events

Dean Del Ponte-2
Yes, sorry.  That works.

My bad.  I didn't notice that the domain class was being instantiated and saved in one step.

Thanks!

- Dean Del Ponte
On Thu, Jun 21, 2012 at 10:46 AM, Aaron Long <[hidden email]> wrote:
Can't you just create an instance of the service and set it manually on the domain object?

def obj = new MyDomain()
obj.service = new MockWhateverService()
obj.save()

-Aaron

On Thu, Jun 21, 2012 at 10:50 AM, Dean Del Ponte <[hidden email]> wrote:
I have a domain class with a service and hibernate events.

The hibernate events call the service.

When unit testing, the service is always null.

How do I either set the service on the domain class or mock it out?

Example:

class MyDomain {
     def service

     afterInsert() {
          service.doSomething()
     }
}

I'm using Spock for testing and have tried:
Mock out the afterInsert method with:
     MyDomain.metaClass.afterInsert{}  <-  This does nothing.

All help is appreciate.

Thanks!

- Dean Del Ponte


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to unit test a Domain class with hibernate events

johnrengelman
This won't work if the CUT is a service that create a domain object instance and calls .save() on it. You don't have a handle to the domain object instance in order to add the mock to it.

We have this same problem because we have a custom dateTimeService that we use to skew time around in our application, so our domain objects use this to set the dataCreated/lastUpdated on the domain object.

So this is our scenario

class Foo {
   def dateTimeService

   afterInsert() {
      dateCreated = dateTimeService.now
   }
}

class FooService {
   public void bar() {
      new Foo().save()
   }
}

@TestFor(FooService)
class FooServiceTests {

   @Test public void testBar() {   fooService.bar() } //Throws a NPE


We haven't found a good way to test this yet. You could metaClass replace the callback on the Domain class so every subsequent instance gets it and then test the callback in a separate test. This is probably the best option. Alternatively you could do an integration test rather than a unit test. I'd be interested if there's another suggestion.

--
John

On Thursday, June 21, 2012 at 10:56 AM, Dean Del Ponte wrote:

Yes, sorry.  That works.

My bad.  I didn't notice that the domain class was being instantiated and saved in one step.

Thanks!

- Dean Del Ponte
On Thu, Jun 21, 2012 at 10:46 AM, Aaron Long <[hidden email]> wrote:
Can't you just create an instance of the service and set it manually on the domain object?

def obj = new MyDomain()
obj.service = new MockWhateverService()
obj.save()

-Aaron

On Thu, Jun 21, 2012 at 10:50 AM, Dean Del Ponte <[hidden email]> wrote:
I have a domain class with a service and hibernate events.

The hibernate events call the service.

When unit testing, the service is always null.

How do I either set the service on the domain class or mock it out?

Example:

class MyDomain {
     def service

     afterInsert() {
          service.doSomething()
     }
}

I'm using Spock for testing and have tried:
Mock out the afterInsert method with:
     MyDomain.metaClass.afterInsert{}  <-  This does nothing.

All help is appreciate.

Thanks!

- Dean Del Ponte



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to unit test a Domain class with hibernate events

Ian Roberts
On 21/06/2012 19:58, John Engelman wrote:
> We haven't found a good way to test this yet. You could metaClass
> replace the callback on the Domain class so every subsequent instance
> gets it and then test the callback in a separate test. This is probably
> the best option. Alternatively you could do an integration test rather
> than a unit test. I'd be interested if there's another suggestion.

How about a metaclass override of the constructor rather than the callback:

Foo.metaClass.constructor = {->
  def foo = BeanUtils.instantiateClass(Foo)
  foo.dateTimeService = [now:new Date()]
  return foo
}

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Loading...