2

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>
Akash Mishra
  • 31
  • 1
  • 3
  • 3
    Here is a similar question https://stackoverflow.com/questions/52830441/junit5-mock-a-static-method – Kris Jul 21 '22 at 20:02
  • The first question would be: Why do you need to mock static methods? Can you given an example? – khmarbaise Jul 27 '22 at 08:07

0 Answers0