vendredi 2 septembre 2016

How to mock Android classes with PowerMock?

Can I use PowerMock to mock Android classes called from with the class under test?

Example:

private static void appendProduct(StringBuilder builder, Product product) {
    Time time = product.getTime();
    // Product#getTime() calls Time#setNow() internally
    String formattedTime = DateUtils.getFormattedDateTime(time);
    builder.append(product.getTitle());
    builder.append(formattedTime);
}

Normally, this cannot be tested because an Android class is invoked. The following error is shown when I try nonetheless:

RuntimeException: Method setToNow in android.text.format.Time not mocked.

In my test I do not care what getTime() returns. It would be great if I can mock the return value.

Aucun commentaire:

Enregistrer un commentaire