0

I want to override a previously defined expectation in JMockit. This is what I tried (see code below) -- I have a private class where I record all the common expectations and I replay it in various test methods. However, one of my method needs most of the common expectations except for few. First, I am calling the CommonNonStrictExpectations private class and then defining test specific expectations in my testMethod1 with a hope that what I defined here overrides what I have defined earlier. I dont think this way of overriding works, is there a way that works?

//MyClassTest.java
    import MyClass;

    public class MyClassTest {

    @Mocked Someobject object;    

    @Test
    public void testMethod1() throws Exception {
        new CommonNonStrictExpectations() {};
        new NonStrictExpectations() {
            {
                object.getInt(anyInt); returns (-1);
                object.getString(anyInt); returns ("failure");
            }
        };
        System.out.println("position: " + object.getInt(1));
        System.out.println("exec status: " + object.getString(1));
        MyClass m = new MyClass();
        m.method(object, -1);
    }

    private class CommonNonStrictExpectations extends NonStrictExpectations {
        public CommonNonStrictExpectations () throws Exception {
            object.getInt(anyInt); returns (anyInt);
            object.getString(anyInt); returns ("success");
        }
    }
}

//MyClass.java

import Someobject;
public class MyClass {

    public void method (Someobject someobject, int i) {
        String status = someobject.getString(i);
        if (status.equalsIgnoreCase("success")) {
            print(someobject, "success");
        }  else if (status.equalsIgnoreCase("failure")) {
            print(someobject, "failure");
        } else
            print(someobject, "");
    }

    private String print(Someobject someobject, String status) {
        return someobject.printMessage (status);
    }
}

// Someobject.java

public class Someobject {
    public String getString(int i) {
        if (i < 0)
            return "failure";
        else if (i > 0)
            return "success";
        else
            return "";
    }

    public int getInt(int k) {
        return k;
    }

    public String printMessage (String status) {
        return  "status is: " + status;
    }
}
APIgeek
  • 1
  • 2
  • Your example test works just fine for me. A call to `object.getSomemethod(...)` does return the second value recorded for it (ie, "failure"). Could you show a complete example test which fails? – Rogério Oct 18 '13 at 13:28
  • I have shown a better example to illustrate my problem. When I record `object.getInt(anyInt); returns (anyInt)` inside the `CommonNonStrictExpectations` class, overriding has no effect. Instead, when I record `object.getInt(anyInt); returns (1)`, it does override. – APIgeek Oct 20 '13 at 14:29
  • Ah, that's it then: `returns(anyInt)` is not valid usage, since argument matchers like `anyInt` are only meant for parameters, not for return values. If you want to say that some method can return any value, then simply do not specify any result for it; or better yet, do not even record an expectation for it. – Rogério Oct 21 '13 at 14:20

0 Answers0