Java PowerMockito static方法/new对象/public方法/private方法/public属性/private属性

Java PowerMockito static方法/new对象/public方法/private方法/public属性/private属性

  • [1 变量Mock](#1 变量Mock)
    • [1.1 公有变量](#1.1 公有变量)
    • [1.2 私有变量](#1.2 私有变量)
    • [1.3 公有静态变量](#1.3 公有静态变量)
    • [1.4 私有静态变量](#1.4 私有静态变量)
  • [2 方法Mock](#2 方法Mock)
    • [2.1 共有方法](#2.1 共有方法)
    • [2.2 私有方法](#2.2 私有方法)
    • [2.3 共有静态方法](#2.3 共有静态方法)
    • [2.4 共有最终方法](#2.4 共有最终方法)
  • [3 new对象Mock](#3 new对象Mock)
    • [3.1 共有方法](#3.1 共有方法)

1 变量Mock

java 复制代码
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 公有变量

java 复制代码
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 私有变量

java 复制代码
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 公有静态变量

java 复制代码
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 私有静态变量

java 复制代码
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

java 复制代码
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 共有方法

java 复制代码
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 私有方法

java 复制代码
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 共有静态方法

java 复制代码
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 共有最终方法

java 复制代码
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());
        // 模拟 final 方法的返回值
        PowerMockito.when(impl.publicFinalMethod("Test")).thenReturn("MockedTest");
        // 调用 final 方法
        String str = impl.publicFinalMethod("Test");
        // 验证结果
        Assert.assertEquals("MockedTest", str);
    }

}

3 new对象Mock

java 复制代码
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 共有方法

java 复制代码
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);
    }

}
相关推荐
yzhSWJ6 分钟前
mybatisplu设置自动填充
java·spring·tomcat
Java雪荷10 分钟前
基于 Vant UI + Redisson BitSet 实现签到日历
java·redis·vue
白宇横流学长30 分钟前
基于Javaee的影视创作论坛的设计与实现【源码+文档+部署讲解】
java·java-ee
南宫生39 分钟前
力扣-数据结构-4【算法学习day.75】
java·数据结构·学习·算法·leetcode
TANGLONG2221 小时前
【初阶数据结构与算法】八大排序算法之归并排序与非比较排序(计数排序)
java·数据结构·c++·算法·面试·蓝桥杯·排序算法
winks31 小时前
使用RabbitMQ
java·rabbitmq·java-rabbitmq
Oneforlove_twoforjob1 小时前
【Java基础面试题044】使用new String(“哈哈“)语句会创建几个对象?
java·开发语言
坊钰1 小时前
【Java 数据结构】LinkedList 类 和 模拟实现链表
java·开发语言·数据结构·学习·算法·链表
胡尔摩斯.1 小时前
SpringMVC
java·开发语言·后端·spring·代理模式
未来并未来1 小时前
深入解析MVCC中Undo Log版本底层存储读取逻辑
java·数据库·mysql