1

Setting up some integration tests, I'm having issues with domain class equality. The equality works as expected during normal execution, but when testing the Service methods through an integration test, the test for equality is coming back false.

One service (called in the setUp() of the Test Case) puts a Domain object into the session

SomeService {
  setSessionVehicle(String name) {
    Vehicle vehicle = Vehicle.findByName(name)
    session.setAttribute("SessionVehicle", vehicle)
  }

  getSessionVehicle() {
    return session.getAttribute("SessionVehicle")
  }
}

Elsewhere in another service, I load an object and make sure the associated attribute object matches the session value:

OtherService {
  getEngine(long id) {
    Vehicle current = session.getAttribute("SessionVehicle")

    Engine engine = Engine.get(id)
    if(!engine.vehicle.equals(current)) throw Exception("Blah blah")
  }
}

This works as expected during normal operation, preventing from loading the wrong engine (Ok, I sanitized the class names, pretend it makes sense). But in the integration test, that .equals() fails when it should succeed:

Vehicle testVehicle

setUp() {
  Vehicle v = new Vehicle("Default")
  v.save()
  someService.setSessionVehicle("Default")
  testVehicle = someService.getSessionVehicle()
}

testGetEngine() {
  List<Engine> engines = Engine.findAllByVehicle(testVehicle)
  //some assertions and checks
  Engine e = otherService.getEngine(engines.get(0).id)
}

The findAll() call is correctly returning the list of all Engines associated with the vehicle in the session, but when I try to look up an individual Engine by ID, the equality check for session Vehicle vs Vehicle on the found Engine fails. Only a single vehicle has been created at this point and the Exception message displays that the session Vehicle and the Engine.Vehicle exist and are the same value.

If I attempt this equality check in the testCase itself, it fails, but I'm able to change the testCase to check if(vehicle.id == sessionVehicle.id) which succeeds, but I'm not keen on changing my production code in order to satisfy an integration test.

Am I doing something wrong when setting up these domain objects in my test case that I should be doing differently?

4

1 回答 1

4

First of all, the equality check you are doing is just checking the reference. You should not use the default equals method for your check, better override the equals method in domain class.

There are two ways you can override the equals method:
1) you can use your IDE to auto-generate code for equals method(a lot of null checking etc..).
2) Preferred way: You can use EqualsBuilder and HashCodeBuilder classes from the Apache Commons project. The library should be already available to your application, or download the JAR file and place in lib.
Here is sample code for using EqualsBuilder:

            boolean equals(o) {
               if ( !(o instanceof Vehicle) ) {
                  return false
               }
               def eb = new EqualsBuilder()
               eb.append(id, o.id)
               eb.append(name, o.name)
               eb.append(otherProperties, o.otherProperties)
            ....
               return eb.isEquals()
            }

Another point is: how you are getting session in service? from RequestContextHolder? Its a good practice to not access session directly from service, rather send the value as method parameter in the service.

于 2013-05-02T06:03:26.820 回答