How to mock static methods in Junit 5

Viewed 165

I read a few threads here about static methods, and I think I understand the problems misuse/excessive use of static methods can cause. But I didn't really get to the bottom of why it is hard to mock static methods in JUnit5.

I know in JUnit4 we can do it using PowerMockito but how to do in Junit5?

An easy explanation/link would be great.

My UseCase: I've created a service class in which I'm calling static method of other class. But that value is returning as Null.

So, I need some reference by which I can MOck that static method so that I can continue writing test case for same.

JUnit5 dependencies in pom.xml

        <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.8.2</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.8.2</version>
        <scope>test</scope>
    </dependency>
0 Answers
Related