Sitemap
Full Stack Developer

Full Stack Developer

This blog is curated just for you, if you are a Developer planning to appear for interviews OR looking out for new & interesting things happening in Java Space.

How to write JUNIT test cases for Static Classes and Static Methods in Java

2 min readNov 26, 2024

--

It’s not as straightforward as you think, I can tell you that.

Press enter or click to view image in full size
Photo by Melissa Walker Horn on Unsplash

Testing static classes and static methods in Java with JUnit 5 involves understanding how to mock static methods, as they are tied to the class and not to an instance. JUnit 5 itself does not have built-in support for mocking static methods, but you can use external libraries like Mockito (with its mockito-inline module) or PowerMock.

Get Full Stack Developer’s stories in your inbox

Join Medium for free to get updates from this writer.

Here’s a step-by-step guide:

1. Set Up Dependencies

Include the following dependencies in your pom.xml (Maven) or build.gradle (Gradle).

Maven:

<dependencies>
<!-- JUnit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<!-- Mockito -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>
<!-- Mockito Inline for mocking static methods -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>
</dependencies>

Gradle:

dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
testImplementation 'org.mockito:mockito-core:5.6.0'
testImplementation 'org.mockito:mockito-inline:5.6.0'
}

2. Static Class Example

Here is an example of a static class to test:

public class MathUtils {

public static int add(int a, int b) {
return a + b;
}
public static String getEnvironment() {
// Simulate fetching environment details
return "PRODUCTION";
}
}

3. Writing Unit Tests for Static Methods

3.1. Without Mocking

If the static method is straightforward (like MathUtils.add), you can directly test it without mocking:

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class MathUtilsTest {
@Test
void testAdd() {
int result = MathUtils.add(2, 3);
assertEquals(5, result, "2 + 3 should equal 5");
}
}

3.2. With Mocking (Using Mockito)

For methods like MathUtils.getEnvironment that involve external dependencies or side effects, you can mock the behavior.

import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

public class MathUtilsTest {
@Test
void testGetEnvironment() {
// Mocking static method
try (MockedStatic<MathUtils> mockedStatic = mockStatic(MathUtils.class)) {
mockedStatic.when(MathUtils::getEnvironment).thenReturn("TEST");
// Test the mocked behavior
String environment = MathUtils.getEnvironment();
assertEquals("TEST", environment);
// Verify the static method was called
mockedStatic.verify(MathUtils::getEnvironment, times(1));
}
}
}

4. Test Output

When running the tests:

  • For non-mocked tests, the actual method implementation will execute.
  • For mocked tests, the mocked behavior will execute as defined.

5. Tips for Testing Static Methods

  1. Avoid Overusing Static Methods: Use static methods for utility functions only. Dependency injection is easier to test.
  2. Use MockedStatic Closely: Ensure the try block is used to close the mocked context.
  3. Verify Calls: Always verify that your mocked static method was called the expected number of times.

6. Advanced Scenarios

If the static methods interact with databases, external APIs, or configurations, mock these interactions:

  • Use MockedStatic for mocking.
  • Combine with @ExtendWith(MockitoExtension.class) for complex setups.

With this, you should be well-equipped to test static classes and methods effectively in JUnit 5!

Hope this helps!!!!

--

--