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());
        // 模拟 final 方法的返回值
        PowerMockito.when(impl.publicFinalMethod("Test")).thenReturn("MockedTest");
        // 调用 final 方法
        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());
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值