Home > other >  How to mock the BooleanUtils class or an application property?
How to mock the BooleanUtils class or an application property?

Time:09-05

I'm trying to create a when sentence, but I can't do it well because I don't know how to mock correctly whatever.

I have this code:

public class OperationMovement {

@Value("${operation.check}")
private Boolean needToCheck;

   private void checkOperation() {

      // Code

      if (BooleanUtils.isTrue(needToCheck)) {
         // More code
      }
   }
}

I need to create a test with Mockito, but I don't know how to mock this if else.

I have tried to mock the BooleanUtils this way:

@Mock
BeanUtils beanUtils;

// Code and more testing code

when(booleanUtils.isTrue(anyBoolean())).thenReturn(true);

But this returs an error.

I have tried the following too, but I have the same error:

when(BooleanUtils.isTrue(anyBoolean())).thenReturn(true);

I need to mock that propertie or the BooleanUtils class, but I don't know how.

CodePudding user response:

Quick example usage:

private OperationMovement classUnderTest;
...

@Test
void testOperationIsTrue() {
    // For this test case, you are setting it to true
    ReflectionTestUtils.setField(classUnderTest,"needToCheck",true);
}
  • Related