I am facing a weird behavior When using mockedstatic with mockedConstruction. Debugger highlights wrong lines but If I added System.out.println I see that the code executes properly.

here is the code

public class Example {


  private AnotherClass obj;
  
  public Example(Context context)
  {
    obj= new AnotherClass(context);
  }
  public static boolean isItValid(int num) {
    return (num > 5);

  }

  public static boolean matchesTheLimits(int num) {
    return (num > 100);
  }
  
  
  public void accepts(int num)
  {
    return isItValid(num) && matchesTheLimits(num) && obj.isItValidContext() ;
  }
}

I want to test accepts method, I am mocking context while doing this unit test.

here are the code for the unit test

@Test
public void testAccepts() {


  try (MockedConstruction<AnotherClass> mocked =
      Mockito.mockConstruction(AnotherClass.class, (mock, context) -> {

        when(mock.isItValidContext()).thenReturn(true);

      })) {

    try (MockedStatic<Example> mockedStatic =
        Mockito.mockStatic(Example.class)) {

      mockedStatic
          .when(() -> Example
              .isItValid( Mockito.anyInt()))
          .thenReturn(true);

      mockedStatic
      .when(() -> Example
          .matchesTheLimits( Mockito.anyInt()))
      .thenReturn(true);

      Example example = new Example(mockContext);

      boolean result = example.accepts(5);

      Assert.assertTrue(result);
    }
  }
}

am I doing anything wrong?

Thanks alot.

0

There are 0 best solutions below