keeganwitt / gmock

Automatically exported from code.google.com/p/gmock
6 stars 2 forks source link

Class Cast Exception using Grails Domain Class #69

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Download drools-grails.tgz from
http://notesmine.com/_media/drools-grails.tgz

2. Un-tar the file, and CD into drools-grails 

3. Run grails test-app com.droolsrule.DroolsRule

4. Open test/reports/TEST-com.droolsrule.DroolsRuleTests.xml, and you'll
see a ClassCastException

Example:

 <error message="org.gmock.internal.recorder.ReturnMethodRecorder"
type="java.lang.ClassCastException">java.lang.ClassCastException:
org.gmock.internal.recorder.ReturnMethodRecorder
    at
com.droolsgrails.Notification$$EnhancerByCGLIB$$bf1a5218.hashCode(&lt;generated&
gt;)
    at
org.drools.common.EqualityKeyComparator.hashCodeOf(EqualityKeyComparator.java:40
)
    at org.drools.util.ObjectHashMap.get(ObjectHashMap.java:102)
    at
org.drools.common.TruthMaintenanceSystem.get(TruthMaintenanceSystem.java:82)

What is the expected output? What do you see instead?
Expected output is TESTS PASSED

5. See the test code in test/integration/com/droolsrule/DroolsRuleTests.groovy
   * Test code tries to mock a simple Grails Domain class, and use it
     in a Drools Rule.
   * Drools evaluates the rule, and throws a class cast exception.
   * GMock is able to mock Java classes such as java.lang.String in the
     Drools Rule, but it is not able mock Grails Domain classes.

What version of the product are you using? On what operating system?
GMock 0.6.0
OSX Leopard
Grails 1.0.3
Groovy 1.5.6
JVM 1.5.0_16-133

Please provide any additional information below.

I realize that this problem could be occurring due to Grails and/or the
Drools plugin, but thought that the authors of the GMock software might
be able to spot a problem.  Any help is appreciated.

Original issue reported on code.google.com by Nathan.N...@gmail.com on 3 Mar 2009 at 10:03

GoogleCodeExporter commented 9 years ago
Thanks for this extremely details report. We'll investigate it.

Original comment by julien.g...@gmail.com on 3 Mar 2009 at 11:16

GoogleCodeExporter commented 9 years ago
I have had a look at the test, it seems that you didn't use gmock correctly. You
should setup the expectations first(outside the play closure), and then run the 
test
inside the play closure.

So, instead of

  def notifi = mock(com.droolsgrails.Notification)
  def objList = [ notifi ]
  def globalList = []
  def rule = createDummyRule()
  droolsService.fireRule(rule, objList, globalList)

it should be

  def notifi = mock(com.droolsgrails.Notification)
  // expectations of notifi can be set here
  def objList = [ notifi ]
  def globalList = []
  def rule = createDummyRule()
  play {
    droolsService.fireRule(rule, objList, globalList)
  }

I changed the test as above, then it passed.

Original comment by JohnnyJianHY on 4 Mar 2009 at 7:50

GoogleCodeExporter commented 9 years ago
Thank you very much for your advice!

It works great!

Original comment by Nathan.N...@gmail.com on 6 Mar 2009 at 3:40