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);
    }

}
相关推荐
程序员-珍4 分钟前
使用openapi生成前端请求文件报错 ‘Token “Integer“ does not exist.‘
java·前端·spring boot·后端·restful·个人开发
2401_8572979131 分钟前
招联金融2025校招内推
java·前端·算法·金融·求职招聘
福大大架构师每日一题41 分钟前
23.1 k8s监控中标签relabel的应用和原理
java·容器·kubernetes
金灰1 小时前
HTML5--裸体回顾
java·开发语言·前端·javascript·html·html5
菜鸟一皓1 小时前
IDEA的lombok插件不生效了?!!
java·ide·intellij-idea
爱上语文1 小时前
Java LeetCode每日一题
java·开发语言·leetcode
bug菌1 小时前
Java GUI编程进阶:多线程与并发处理的实战指南
java·后端·java ee
程序猿小D2 小时前
第二百六十九节 JPA教程 - JPA查询OrderBy两个属性示例
java·开发语言·数据库·windows·jpa
极客先躯2 小时前
高级java每日一道面试题-2024年10月3日-分布式篇-分布式系统中的容错策略都有哪些?
java·分布式·版本控制·共识算法·超时重试·心跳检测·容错策略
夜月行者3 小时前
如何使用ssm实现基于SSM的宠物服务平台的设计与实现+vue
java·后端·ssm