0

I'm trying to mock a web call that returns different results depending on the inputs.

My test case is as follows:

class RestService1 implements IRestService{

    public static String checkSomething(){
         return "something";
     }
}


class RestService2 implements IRestService{

    public static String checkSomething(){
        return "somethingElse";

    }
}


class TestClass {

    void test(){
        final RestService1 restServiceMock1=new RestService();

        new NonStrictExpectations() {
            @SuppressWarnings("unused")
            WebAPI webAPI; 
            {
                webAPI.getHandle( IRestService.class );
                result=restServiceMock1;
            }


        };

    String check=webAPI.getHandle.getSomething();

    //here check should have "something"

    //modify some data received as output

    final RestService2 restServiceMock2=new RestService();

    new NonStrictExpectations() {

        @SuppressWarnings("unused")

        WebAPI webAPI; 
        {
            webAPI.getHandle( IRestService.class );
            result=restServiceMock2;
        }

     };
     String check2=webAPI.getHandle.getSomething()
     //here check2 should have "somethingElse"

} 

Note: webAPI.getHandle() returns a fully loaded instance of IRestService that does webCall.

This testcase is giving me Duplicate Expectation error when run through maven using --javaagent:{jar location} However runs without any issues and as expected when run through Eclipse.

Is there some way to remove the expectation set first or some other change that can be done to override the initial Expectation with the next one?

Thewads
  • 4,953
  • 11
  • 55
  • 71

1 Answers1

0

The test method needs to be separated into two tests, one for RestService1 and another for RestService2.

Rogério
  • 16,171
  • 2
  • 50
  • 63
  • I was wondering if there is any way of overriding an existing expectation without splitting the test case or if i can remove an expectation half way through a testcase and then create a new one. The code I've depicted here is just a basic look of my actual code. In reality I require the output from the RestService1 when I call API that uses RestService2. Hope I have'nt confused you now :) – user3917967 Aug 08 '14 at 07:27
  • Also why is overriding working fine in eclipse but failing in maven - surefire? – user3917967 Aug 08 '14 at 10:10
  • It's running fine for me, from Maven. Can you show a compilable test class plus a pom.xml file, which will fail when executed? – Rogério Aug 08 '14 at 18:57