0

Sample class

public class Test{
@Tested ClassA objA;

@Test(expected = MyException.class){
  String expectedVar = "expectedVar";
  new Expectations(objA)
  {
    {
      objA.someMethod();
      result = expectedVar;
    }
  };

  // So here is error, when I debug the programm it doesn't even enter following method.
  // If I connent out new Expectations(){{...}} block, only then the programm
  // will enter the method objA.execute()
  objA.execute();
}

Could anyone explain what is happening here and why setting expectations on some method changes behaviour of other method?

Arsen Simonean
  • 362
  • 2
  • 17

2 Answers2

0

I didn't find an answer, so I did it the other way:

new MockUp<ClassA>()
{   
    @Mock
    String someMethod()
    {
        return expectedVar;
    }
};

And now it worked as expected

Arsen Simonean
  • 362
  • 2
  • 17
0

Actually, the test works fine. Run the following complete example, and it will pass:

public class ExampleTest {
    static class ClassA {
        String someMethod() { return ""; }

        void execute() {
            if ("expectedVar".equals(someMethod())) throw new MyException();
        }
    }

    static class MyException extends RuntimeException {}

    @Tested ClassA objA;

    @Test(expected = MyException.class)
    public void exampleTest() {
        final String expectedVar = "expectedVar";

        new Expectations(objA) {{ objA.someMethod(); result = expectedVar; }};

        objA.execute();
    }
}
Rogério
  • 16,171
  • 2
  • 50
  • 63