add powermock
This commit is contained in:
@@ -3,7 +3,9 @@ package me.ehlxr.powermock;
|
|||||||
/**
|
/**
|
||||||
* Created by lixiangrong on 2017/11/3.
|
* Created by lixiangrong on 2017/11/3.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by lixiangrong on 2017/7/21.
|
* Created by lixiangrong on 2017/7/21.
|
||||||
*/
|
*/
|
||||||
@@ -11,34 +13,43 @@ public class ClassUnderTest {
|
|||||||
public boolean callArgumentInstance(File file) {
|
public boolean callArgumentInstance(File file) {
|
||||||
return file.exists();
|
return file.exists();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean callInternalInstance(String path) {
|
public boolean callInternalInstance(String path) {
|
||||||
File file = new File(path);
|
File file = new File(path);
|
||||||
return file.exists();
|
return file.exists();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean callFinalMethod(ClassDependency refer) {
|
public boolean callFinalMethod(ClassDependency refer) {
|
||||||
return refer.isAlive();
|
return refer.isAlive();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean callSystemFinalMethod(String str) {
|
public boolean callSystemFinalMethod(String str) {
|
||||||
return str.isEmpty();
|
return str.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean callStaticMethod() {
|
public boolean callStaticMethod() {
|
||||||
return ClassDependency.isExist();
|
return ClassDependency.isExist();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String callSystemStaticMethod(String str) {
|
public String callSystemStaticMethod(String str) {
|
||||||
return System.getProperty(str);
|
return System.getProperty(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean callPrivateMethod() {
|
public boolean callPrivateMethod() {
|
||||||
return isExist();
|
return isExist();
|
||||||
}
|
}
|
||||||
public boolean callVoidPrivateMethod(){
|
|
||||||
|
public boolean callVoidPrivateMethod() {
|
||||||
testVoid();
|
testVoid();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isExist() {
|
private boolean isExist() {
|
||||||
// do something
|
// do something
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
private void testVoid(){
|
|
||||||
|
private void testVoid() {
|
||||||
System.out.println("do nothing");
|
System.out.println("do nothing");
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -9,8 +9,6 @@ import org.powermock.modules.junit4.PowerMockRunner;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by lixiangrong on 2017/7/21.
|
* Created by lixiangrong on 2017/7/21.
|
||||||
*/
|
*/
|
||||||
@@ -75,7 +73,7 @@ public class TestClassUnderTest {
|
|||||||
public void testCallPrivateMethod() throws Exception {
|
public void testCallPrivateMethod() throws Exception {
|
||||||
ClassUnderTest underTest = PowerMockito.mock(ClassUnderTest.class);
|
ClassUnderTest underTest = PowerMockito.mock(ClassUnderTest.class);
|
||||||
PowerMockito.when(underTest.callPrivateMethod()).thenCallRealMethod();
|
PowerMockito.when(underTest.callPrivateMethod()).thenCallRealMethod();
|
||||||
PowerMockito.when(underTest, "isExist", anyString()).thenReturn(true);
|
PowerMockito.when(underTest, "isExist").thenReturn(true);
|
||||||
Assert.assertTrue(underTest.callPrivateMethod());
|
Assert.assertTrue(underTest.callPrivateMethod());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user