Last active
May 17, 2019 17:57
-
-
Save leosabbir/10b6c5d685f3148a43fde101b0958ae4 to your computer and use it in GitHub Desktop.
class to test mocking of private and public static method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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