-1

I have statements like

public void dummyMethod() {    
    CreateTableRequest ctr = dynamoDBMapper.generateCreateTableRequest(RolePolicies.class);
    ctr.setProvisionedThroughput(new ProvisionedThroughput(30L, 5L));
    TableUtils.createTableIfNotExists(amazonDynamoDB, ctr);
}

I am trying to create UT for it with :

    @PrepareForTest(TableUtils.class)
    @Test
    public void verifyRoleTableOnDynamo() throws Exception
    {
        Mockito.doReturn(new CreateTableRequest()).when(dynamoDBMapper).generateCreateTableRequest(any());
        Mockito.doReturn(new CreateTableResult()).when(amazonDynamoDB).createTable(ctr);

        PowerMockito.mockStatic(TableUtils.class);
        Mockito.when(tableUtils_mock.createTableIfNotExists(mock(AmazonDynamoDB.class),mock(CreateTableRequest.class)))
            .thenReturn(true);  //This line throws Exception
      // PowerMockito.doReturn(true).when(TableUtils.createTableIfNotExists(amazonDynamoDB,ctr));

        testClassObject.dummyMethod();

        //There should be no Exception in this case.
        Assert.assertTrue(true); // Other Assert
    }

Instead of mocking and returning the value, PowerMockito calls actual implementation of method createTableIfNotExists:

I get error:

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: Boolean cannot be returned by createTable() createTable() should return CreateTableResult

FYI: Amazon SDK implements it as:

public static final boolean createTableIfNotExists(final AmazonDynamoDB dynamo, final CreateTableRequest createTableRequest) {
    try {
        dynamo.createTable(createTableRequest);
        return true;
    } catch (final ResourceInUseException e) {
        //Statements
    }
    return false;
}
MalTec
  • 1,350
  • 2
  • 14
  • 33

1 Answers1

0

@PrepareForTest is added at the class level, not method

@RunWith(PowerMockRunner.class)
@PrepareForTest(TableUtils.class) //<-- Add @PrepareForTest at class level.
public class MyTest {    
    @Test
    public void verifyRoleTableOnDynamo() throws Exception {
        //Arrange
        PowerMockito.mockStatic(TableUtils.class); //<-- Call PowerMockito.mockStatic() to mock a static class

        //Just use Mockito.when() to setup your expectation:
        Mockito.when(TableUtils.createTableIfNotExists(any(AmazonDynamoDB.class), any(CreateTableRequest.class)))
            .thenReturn(true).;

        //Act
        testClassObject.dummyMethod();

        //There should be no Exception in this case.
        Assert.assertTrue(true); // Other Assert
    }
}

Reference Mocking Static Method

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Have tried that first. Had same result as shared earlier. Here the method is static final. – MalTec Oct 25 '18 at 10:42
  • @MalTec you noticed that I am using different argument matchers? If the mocked static member does not match the configured expectations it will default back to the actual member invocation. I suggest you recheck the expectation setup. – Nkosi Oct 25 '18 at 11:13