Techiio-author
Started by Peter DanielsSep 28, 2021

Closed
How to mock UUID.randomUuid() and System.currentTimeMillis() without PowerMock?

5 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 5 VIEWES SHARE

sobI am improving my code coverage, and I am using Sonar to compute it. But Sonar and PowerMock are not very compatible. I was wondering if there is another way to mock UUID.randomUUID() and System.currentTimeMillis() methods without using PowerMock?

2 Replies

Techiio-commentatorNeel Dey replied 4 months ago0 likes0 dislikes

One should introduce dependencies instead of relying on static method calls. Then you won't need power mock at all.

Assuming your tools are JUnit5, assertj, and mockito. An example goes like this:

// -- production code --
import java.util.UUID;
import java.util.function.Supplier;
public class ProductionCode {
    private final Supplier<String> idSupplier;
    private final Supplier<Long> clock;
    // @VisibleForTesting
    ProductionCode(Supplier<String> idSupplier, Supplier<Long> clock) {
        this.idSupplier = idSupplier;
        this.clock = clock;
    }
    public ProductionCode() {
        this(() -> UUID.randomUUID().toString(), System::currentTimeMillis);
    }
    public String methodUnderTest() {
        return String.format("%s -- %d", idSupplier.get(), clock.get());
    }
}
// -- test code without mocking
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
public class Tests {
    @Test
    void methodUnderTest_shouldSeparateByDoubleDashRandomIdAndCurrentTime() {
        ProductionCode objectUnderTest = new ProductionCode(() -> "id", () -> 1234L);
        String s = objectUnderTest.methodUnderTest();
        assertThat(s).isEqualTo("id -- 1234");
    }
}
// -- test code (mockito) --
import static org.assertj.core.api.Assertions.assertThat;
import java.util.function.Supplier;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
public class Tests {
    private final Supplier<String> idSupplier = Mockito.mock(Supplier.class);
    private final Supplier<Long> clock = Mockito.mock(Supplier.class);
    private final ProductionCode objectUnderTest = new ProductionCode(idSupplier, clock);
    @Test
    void methodUnderTest_shouldSeparateRandomIdAndCurrentTimeByDoubleDash() {
        Mockito.when(idSupplier.get()).thenReturn("id");
        Mockito.when(clock.get()).thenReturn(1234L);
        String s = objectUnderTest.methodUnderTest();
        assertThat(s).isEqualTo("id -- 1234");
    }
}
Techiio-commentatorPeter Daniels replied 4 months ago0 likes0 dislikes

Thanks for your feedback.

This thread is closed by Peter Daniels
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance