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

}
相关推荐
芒果披萨13 分钟前
El表达式和JSTL
java·el
duration~1 小时前
Maven随笔
java·maven
zmgst1 小时前
canal1.1.7使用canal-adapter进行mysql同步数据
java·数据库·mysql
跃ZHD1 小时前
前后端分离,Jackson,Long精度丢失
java
blammmp1 小时前
Java:数据结构-枚举
java·开发语言·数据结构
暗黑起源喵2 小时前
设计模式-工厂设计模式
java·开发语言·设计模式
WaaTong2 小时前
Java反射
java·开发语言·反射
九圣残炎2 小时前
【从零开始的LeetCode-算法】1456. 定长子串中元音的最大数目
java·算法·leetcode
wclass-zhengge2 小时前
Netty篇(入门编程)
java·linux·服务器
Re.不晚3 小时前
Java入门15——抽象类
java·开发语言·学习·算法·intellij-idea