5

Having the following class:

class ToTest{
    @Autowired
    private Service service;

    public String make(){
         //do some calcs
         obj.setParam(param);
         String inverted = service.execute(obj);
         return "<" + inverted.toString() + ">";
    }
}

I'd like to add a test that asserts me that service.execute is called with an object with the param X.

I'd do that with a verification. I want to mock this call and make it return something testable. I do that with an expectations.

@Tested
ToTest toTest;
@Injected
Service service;

new NonStrictExpectations(){
   {   
       service.exceute((CertainObject)any)
       result = "b";
   }
};

toTest.make();

new Verifications(){
   {   
       CertainObject obj;
       service.exceute(obj = withCapture())
       assertEquals("a",obj.getParam());
   }
};

I get a null pointer on obj.getParam(). Apparently the verification does not work. If I remove the expectation it works but then I get a null pointer in inverted.toString().

How would you guys make this work?

Jordi P.S.
  • 3,838
  • 7
  • 36
  • 59

1 Answers1

1

The following test class is working fine for me, using JMockit 1.4:

public class TempTest
{
    static class CertainObject
    {
        private String param;
        String getParam() { return param; }
        void setParam(String p) { param = p; }
    }

    public interface Service { String execute(CertainObject o); }

    public static class ToTest
    {
        private Service service;

        public String make()
        {
            CertainObject obj = new CertainObject();
            obj.setParam("a");
            String inverted = service.execute(obj);
            return "<" + inverted + ">";
        }
    }

    @Tested ToTest toTest;
    @Injectable Service service;

    @Test
    public void temp()
    {
         new NonStrictExpectations() {{
             service.execute((CertainObject) any);
             result = "b";
         }};

         toTest.make();

         new Verifications() {{
             CertainObject obj;
             service.execute(obj = withCapture());
             assertEquals("a", obj.getParam());
         }};
    }
}

Can you show a complete example test which fails?

Rogério
  • 16,171
  • 2
  • 50
  • 63
  • I was just using an old version of JMockit. – Jordi P.S. Dec 05 '13 at 15:27
  • This code is not working with JMockit version 1.23 any more. I'll get the following exception: java.lang.IllegalStateException: Identical expectation already recorded; please remove this verification or adjust the recording ... Caused by: Redundant expectation ... What's the recommended way to define an expectation and do some custom verifications? – Joern May 03 '16 at 09:13
  • There are four different mechanisms that could be used in the test above, all in the expectation recording block, with the verification block removed: 1) use a Hamcrest argument matcher passed in a call to `withArgThat(Hamcrest matcher)`; 2) use a custom matcher in a call to `with(Delegate)`; 3) use `withCapture(List)`; 4) assign `result` with a `Delegate` object which validates the received arguments. Which of these would be the best depends on several factors, including personal preference. The example test doesn't actually need this, of course, as it merely checks the argument is `"a"`. – Rogério May 03 '16 at 15:20