title: Grails integration test - domain object equality
body: <p>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.</p>
  
  <p>One service (called in the setUp() of the Test Case) puts a Domain object into the session</p>
  
  <pre><code>SomeService {
    setSessionVehicle(String name) {
      Vehicle vehicle = Vehicle.findByName(name)
      session.setAttribute("SessionVehicle", vehicle)
    }
  
    getSessionVehicle() {
      return session.getAttribute("SessionVehicle")
    }
  }
  </code></pre>
  
  <p>Elsewhere in another service, I load an object and make sure the associated attribute object matches the session value:</p>
  
  <pre><code>OtherService {
    getEngine(long id) {
      Vehicle current = session.getAttribute("SessionVehicle")
  
      Engine engine = Engine.get(id)
      if(!engine.vehicle.equals(current)) throw Exception("Blah blah")
    }
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>Vehicle testVehicle
  
  setUp() {
    Vehicle v = new Vehicle("Default")
    v.save()
    someService.setSessionVehicle("Default")
    testVehicle = someService.getSessionVehicle()
  }
  
  testGetEngine() {
    List&lt;Engine&gt; engines = Engine.findAllByVehicle(testVehicle)
    //some assertions and checks
    Engine e = otherService.getEngine(engines.get(0).id)
  }
  </code></pre>
  
  <p>The <code>findAll()</code> 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.</p>
  
  <p>If I attempt this equality check in the testCase itself, it fails, but I'm able to change the testCase to check <code>if(vehicle.id == sessionVehicle.id)</code> which succeeds, but I'm not keen on changing my production code in order to satisfy an integration test.</p>
  
  <p>Am I doing something wrong when setting up these domain objects in my test case that I should be doing differently?</p>