0
votes

I'm new to JMock and trying to get a simple unit test working through Scala. The test is mocking an interface and then setting some xpectations before executing a method on the mocked interface.

 val context = new Mockery
 val mockObj= context.mock(classOf[SomeClassInterface])

 @Test def sometest = {
 context.checking(
      new Expectations() {
        allowing (mockObj).doFunc1(); 
        will(returnValue(someResponse);
        allowing (mockObj).doFunc2(someResponse); 
        will(returnValue(someResponse));
        allowing (mockObj).doFunc3(someResponse); 
        will(returnValue("Enabled"));
       }
 )
 var status:String = mockObj.doSomething()
 //context.assertIsSatisfied();
 Assert.assertTrue(status.equalsIgnoreCase("Enabled"))

}

This results in the error;

unexpected invocation; mockObj.doSomething() ......... what happened before this; nothing!

Any ideas what is going wrong?

1
Your expectations do not allow the doSomething method to be called. They only allow doFunc1, doFunc2 and doFunc3 to be called on the mockObj (and you do not call these methods at all, so why allow them?).Michał Politowski
i thought the idea was to only set expectations on methods which might be called as a byproduct of calling doSomething. In my case whenever a call to doSomething happens it then causes the doFunc1 2 & 3 methods to be calledcdugga
Causes how? You say that SomeClassInterface is an interface, so there is no implementation of doSomething. Anyway even if there is an implementation, the whole idea of creating a mock object is to not use it.Michał Politowski

1 Answers

0
votes

Not sure about Scala, but are you missing the double '{{' '}}' syntax? We're very sorry about it, but it's the best we could do in Java.

The outer '{}' is an anonymous subclass, and the inner '{}' is an initialisation block.