2

I have the following method....

public void testa(Car car) {
   em.persist(car);
   car.setEngine(null);

}

in my test i have:

protected final Car mockCar = context.mock(Car.class);

@Test
public void testCar() {
        context.checking(new Expectations() {
            {
                oneOf(em).persist(car);
                oneOf(car).setEngine(null);

                   }
             });
        this.stacker.testa(mockCar);
        context.assertIsSatisfied(); 

}

I run this and i keep getting :

unexpected invocation car.setEngine(null)...

If i remove the code that sets the engine in the code and from the test the tests passes... im totally confused as to why this is happening...

exception:

java.lang.AssertionError: unexpected invocation: car.setEngine(null) no expectations specified: did you... - forget to start an expectation with a cardinality clause? - call a mocked method to specify the parameter of an expectation?

2
  • Does this code even compile? You don't close the Expectations implementation or the call to checking. In addition, your call to testa passes a Car object, but testa doesn't take any parameters. Can you narrow the code down to a specific failure case and then post the entire contents of the file(s) please? Feb 25, 2013 at 16:35
  • yes it all compiles.... i was writing it by hand as i had lots of comments in my code... Feb 25, 2013 at 16:38

1 Answer 1

2

Your problem appears to be that you have two Car objects. You have a car, which you set the expectations on, and a mockCar, which you pass through. Without seeing the definitions of these objects, I can't say for sure, but this is probably the root of your problem.

If this isn't the issue, we're going to need more code. Preferably the entire file(s).

For reference, this compiles fine and passes the tests:

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Test;

public class TestyMcTestTest {
    private final Mockery context = new Mockery();

    private final EntityManager em = context.mock(EntityManager.class);
    private final Stacker stacker = new Stacker(em);
    private final Car mockCar = context.mock(Car.class);

    @Test
    public void testCar() {
        context.checking(new Expectations() {{
            oneOf(em).persist(mockCar);
            oneOf(mockCar).setEngine(null);
        }});
        this.stacker.testa(mockCar);
        context.assertIsSatisfied();
    }

    public interface Car {
        void setEngine(Engine engine);
    }

    public interface Engine { }

    public class Stacker {
        private final EntityManager em;

        public Stacker(EntityManager em) {
            this.em = em;
        }

        public void testa(Car car) {
           em.persist(car);
           car.setEngine(null);
        }
    }

    private interface EntityManager {
        void persist(Object o);
    }
}
2
  • Thanks this helped me somewhat.... i think the problme i was having was due to generica being applied... so instead of mocking the car i have now created a instance and check that instance.. Feb 26, 2013 at 10:35
  • That makes sense. I didn't think about it at the time, but your car isn't a collaborator, so why not instantiate one? Glad I helped. Feb 26, 2013 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.