vendredi 5 août 2016

Verify that a private method was NOT executed JMockit

I have looked around for this question a bit and have not found exactly what I need. I have learned a bit more about JMockit and mocking. Which is good. Seems like everyone wants to know how to make sure something has executed. I would like to learn the opposite.

Ok - I am writing a test to check a not so happy path in a public method. The method that is being tested is void so I can't really assert the results. What I would like to do is verify that a method was NOT executed in this test case.

For example:

class ClassToTest {
  private void method(String arg){}

  public void publicMethod(String arg0, String arg1){
    if(false){
      //this method should never get called.
      method(arg0);
    }
  }
}

class TestingClass{
  @Tested
  private ClassToTest classToTest = new ClassToTest();

  @Test
  public void testCheckingIfPrivateMethodWasCalled(){
    classToTest.publicMethod("string1", "string2");

    new Verifications() {
      {
        //At this point I am trying something like
        Deencapsulation.invoke(classToTest, "method", "string1");
        times = 0; //Also tried maxTimes = 0;
        //Through debug it looks like the invoke is doing what it's named...
        //invoking the private method, I don't want to invoke.
        //How do I check that ClassToTest#method was not called?
      }
    }
  }
}

What I am getting as results for the test case is a java.lang.IllegalStateException: Missing invocation to mocked type at this point; please make sure such invocations appear only after the declaration of a suitable mock field or parameter. Which is on the line of times = 0;.

I know the invoke is executing the private method. I am left scratching my head trying to figure out how to "check" that said method is called with out invoking/executing it.

Thanks.

Aucun commentaire:

Enregistrer un commentaire