0

示例类

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();
}

谁能解释这里发生了什么以及为什么对某些方法设置期望会改变其他方法的行为?

4

2 回答 2

0

我没有找到答案,所以我用另一种方式做了:

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

现在它按预期工作

于 2016-02-08T09:10:57.290 回答
0

实际上,测试工作正常。运行以下完整示例,它将通过:

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();
    }
}
于 2016-02-08T16:36:29.167 回答