Java PowerMockito static方法/new对象/public方法/private方法/public属性/private属性
1 变量Mock
package com.xu.test.service.impl;
import com.xu.test.entity.Student;
public class FiledValue {
public Student publicValue = new Student();
private Student privateValue = new Student();
public static Student publicStaticValue = new Student();
private static Student privateStaticValue = new Student();
public void toStr(Student student) {
System.out.println(student.toString());
System.out.println(publicValue.toString());
System.out.println(privateValue.toString());
System.out.println(publicStaticValue.toString());
System.out.println(privateStaticValue.toString());
}
}
1.1 公有变量
package com.xu.test;
import com.xu.test.entity.Student;
import com.xu.test.service.impl.FiledValue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import org.powermock.reflect.internal.WhiteboxImpl;
import java.lang.reflect.Field;
@RunWith(PowerMockRunner.class)
@PrepareForTest({FiledValue.class})
public class FieldValueTest {
@Test
public void publicValue() throws Exception {
FiledValue impl = PowerMockito.spy(new FiledValue());
impl.publicValue = new Student(1);
impl.toStr(impl.publicValue);
WhiteboxImpl.invokeMethod(impl, "toStr", impl.publicValue);
}
}
1.2 私有变量
package com.xu.test;
import com.xu.test.entity.Student;
import com.xu.test.service.impl.FiledValue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import org.powermock.reflect.internal.WhiteboxImpl;
import java.lang.reflect.Field;
@RunWith(PowerMockRunner.class)
@PrepareForTest({FiledValue.class})
public class FieldValueTest {
@Test
public void privateValue() throws Exception {
Student student = new Student(1);
FiledValue impl = PowerMockito.spy(new FiledValue());
impl.publicValue = student;
Field field = FiledValue.class.getDeclaredField("privateValue");
field.setAccessible(true);
field.set(impl, student);
Whitebox.setInternalState(impl, "privateValue", student);
impl.toStr(impl.publicValue);
WhiteboxImpl.invokeMethod(impl, "toStr", impl.publicValue);
}
}
1.3 公有静态变量
package com.xu.test;
import com.xu.test.entity.Student;
import com.xu.test.service.impl.FiledValue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import org.powermock.reflect.internal.WhiteboxImpl;
import java.lang.reflect.Field;
@RunWith(PowerMockRunner.class)
@PrepareForTest({FiledValue.class})
public class FieldValueTest {
@Test
public void publicStaticValue() throws Exception {
Student student = new Student(1);
FiledValue impl = PowerMockito.spy(new FiledValue());
impl.publicValue = student;
FiledValue.publicStaticValue = student;
impl.toStr(impl.publicValue);
WhiteboxImpl.invokeMethod(impl, "toStr", impl.publicValue);
}
}
1.4 私有静态变量
package com.xu.test;
import com.xu.test.entity.Student;
import com.xu.test.service.impl.FiledValue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import org.powermock.reflect.internal.WhiteboxImpl;
import java.lang.reflect.Field;
@RunWith(PowerMockRunner.class)
@PrepareForTest({FiledValue.class})
public class FieldValueTest {
@Test
public void privateStaticValue() throws Exception {
Student student = new Student(1);
FiledValue impl = PowerMockito.spy(new FiledValue());
impl.publicValue = student;
Field field = FiledValue.class.getDeclaredField("privateStaticValue");
field.setAccessible(true);
field.set(impl, student);
impl.toStr(impl.publicValue);
WhiteboxImpl.invokeMethod(impl, "toStr", impl.publicValue);
}
}
2 方法Mock
package com.xu.test.service.impl;
public class MethodValue {
public String publicMethod(String str) {
return "Public" + str;
}
private String privateMethod(String str) {
return "Private" + str;
}
public static String publicStaticMethod(String str) {
return "Private" + str;
}
public final String publicFinalMethod(String str) {
return "Final" + str;
}
}
2.1 共有方法
package com.xu.test;
import com.xu.test.service.impl.MethodValue;
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;
import org.powermock.reflect.Whitebox;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MethodValue.class})
public class MethodValueTest {
@Test
public void publicMethod() throws Exception {
MethodValue impl = PowerMockito.spy(new MethodValue());
PowerMockito.when(impl.publicMethod(Mockito.anyString())).thenReturn("-Public-");
String str = impl.publicMethod("Test");
Assert.assertEquals(str, "-Public-");
}
}
2.2 私有方法
package com.xu.test;
import com.xu.test.service.impl.MethodValue;
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;
import org.powermock.reflect.Whitebox;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MethodValue.class})
public class MethodValueTest {
@Test
public void privateMethod() throws Exception {
MethodValue impl = PowerMockito.spy(new MethodValue());
PowerMockito.when(Whitebox.invokeMethod(impl, "privateMethod", "Test")).thenReturn("MockedTest");
String str = Whitebox.invokeMethod(impl, "privateMethod", "Test");
Assert.assertEquals("MockedTest", str);
}
}
2.3 共有静态方法
package com.xu.test;
import com.xu.test.service.impl.MethodValue;
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;
import org.powermock.reflect.Whitebox;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MethodValue.class})
public class MethodValueTest {
@Test
public void publicStaticMethod() {
PowerMockito.mockStatic(MethodValue.class);
PowerMockito.when(MethodValue.publicStaticMethod("Test")).thenReturn("MockedTest");
String str = MethodValue.publicStaticMethod("Test");
Assert.assertEquals("MockedTest", str);
}
}
2.4 共有最终方法
package com.xu.test;
import com.xu.test.service.impl.MethodValue;
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;
import org.powermock.reflect.Whitebox;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MethodValue.class})
public class MethodValueTest {
@Test
public void publicFinalMethod() {
MethodValue impl = PowerMockito.spy(new MethodValue());
PowerMockito.when(impl.publicFinalMethod("Test")).thenReturn("MockedTest");
String str = impl.publicFinalMethod("Test");
Assert.assertEquals("MockedTest", str);
}
}
3 new对象Mock
package com.xu.test.service.impl;
import com.xu.test.entity.Student;
public class NewValue {
public Student newPublic(Student student) {
System.out.println(student.toString());
return new Student(3);
}
}
3.1 共有方法
package com.xu.test;
import com.xu.test.entity.Student;
import com.xu.test.service.impl.NewValue;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({NewValue.class})
public class NewValueTest {
@Test
public void newPublic() throws Exception {
Student student = new Student(5);
NewValue impl = PowerMockito.spy(new NewValue());
PowerMockito.whenNew(Student.class).withArguments(3).thenReturn(student);
Student result = impl.newPublic(new Student(1));
Assert.assertEquals(result.getId(), 5);
}
}
4 抽象类
package com.xu.test.service.impl;
import com.xu.test.entity.Student;
public abstract class AbstractClass {
public static Student getSubject() {
return new Student(100);
}
}
4.1 公有静态方法
package com.xu.test;
import com.xu.test.entity.Student;
import com.xu.test.service.impl.AbstractClass;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({AbstractClass.class})
public class AbstractClassTest {
@Test
public void getSubject() {
PowerMockito.mockStatic(AbstractClass.class);
Student student = new Student(200);
PowerMockito.when(AbstractClass.getSubject()).thenReturn(student);
Student result = AbstractClass.getSubject();
Assert.assertEquals(200, result.getId());
}
}