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

}
相关推荐
小叶lr2 分钟前
idea 配置 leetcode插件 代码模版
java·leetcode·intellij-idea
qq_429856577 分钟前
idea启动服务报错Application run failed
java·ide·intellij-idea
瑞雨溪8 分钟前
java中的this关键字
java·开发语言
J不A秃V头A16 分钟前
Redisson 中开启看门狗(watchdog)机制
java·分布式锁·看门狗
草字19 分钟前
uniapp input限制输入负数,以及保留小数点两位.
java·前端·uni-app
李迟20 分钟前
某Linux发行版本无法使用nodejs程序重命名文件问题的研究
java·linux·服务器
MapleLea1f32 分钟前
26届JAVA 学习日记——Day14
java·开发语言·学习·tcp/ip·程序人生·学习方法
没有黑科技38 分钟前
基于web的音乐网站(Java+SpringBoot+Mysql)
java·前端·spring boot
爪哇学长40 分钟前
解锁API的无限潜力:RESTful、SOAP、GraphQL和Webhooks的应用前景
java·开发语言·后端·restful·graphql
刘大浪1 小时前
IDEA 2024安装指南(含安装包以及使用说明 cannot collect jvm options 问题 四)
java