Skip to content

Instantly share code, notes, and snippets.

@leosabbir
Last active May 17, 2019 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leosabbir/10b6c5d685f3148a43fde101b0958ae4 to your computer and use it in GitHub Desktop.
Save leosabbir/10b6c5d685f3148a43fde101b0958ae4 to your computer and use it in GitHub Desktop.
class to test mocking of private and public static method
/* File: MockStaticMethodTest.java
* Created: 5/17/2019
* Author: Sabbir Manandhar
*
* Copyright (c) 2019 Hogwart Inc.
*/
package com.hogwart;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
//=================================================================================================
/**
* @author Sabbir Manandhar
* @version 1.0
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({MockStaticMethod.class})
public class MockStaticMethodTest {
@Test
public void testDiff() throws Exception {
PowerMockito.mockStatic(MockStaticMethod.class);
PowerMockito.doReturn(3).when(MockStaticMethod.class, "diff", Mockito.anyInt(), Mockito.anyInt());
MockStaticMethod obj = new MockStaticMethod();
Assert.assertEquals(3, obj.getResult(0, 0, false));
Assert.assertEquals(3, obj.getResult(1, 2, false));
} // testDiff
//-----------------------------------------------------------------------------------------------
@Test
public void testSumWrong() throws Exception {
PowerMockito.mockStatic(MockStaticMethod.class);
PowerMockito.doReturn(100).when(MockStaticMethod.sum(Mockito.anyInt(), Mockito.anyInt()));
MockStaticMethod obj = new MockStaticMethod();
Assert.assertEquals(100, obj.getResult(0, 0, true));
Assert.assertEquals(100, obj.getResult(1, 2, true));
} // testSumWrong
//-----------------------------------------------------------------------------------------------
@Test
public void testSumRight() throws Exception {
PowerMockito.mockStatic(MockStaticMethod.class);
PowerMockito.when(MockStaticMethod.sum(Mockito.anyInt(), Mockito.anyInt())).thenReturn(100);
MockStaticMethod obj = new MockStaticMethod();
Assert.assertEquals(100, obj.getResult(0, 0, true));
Assert.assertEquals(100, obj.getResult(1, 2, true));
} // testSumRight
//-----------------------------------------------------------------------------------------------
@Test
public void testSumRight2() throws Exception {
PowerMockito.mockStatic(MockStaticMethod.class);
PowerMockito.doReturn(100).when(MockStaticMethod.class, "sum", Mockito.anyInt(), Mockito.anyInt());
MockStaticMethod obj = new MockStaticMethod();
Assert.assertEquals(100, obj.getResult(0, 0, true));
Assert.assertEquals(100, obj.getResult(1, 2, true));
} // testSumRight2
} // MockStaticMethodTest
//=================================================================================================
class MockStaticMethod {
public int getResult(int a, int b, boolean sum) {
if (sum) {
return sum(a, b);
} else {
return diff(a, b);
}
} // getResult
//-----------------------------------------------------------------------------------------------
public static int sum(int a, int b) {
return a + b;
} // sum
//-----------------------------------------------------------------------------------------------
private static int diff(int a, int b) {
return a - b;
} // diff
} // MockStaticMethod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment