(工作笔记)jmockit的使用-mock私有变量、静态变量、代码块

目录

  • 一、jmockit配置
  • 二、实例
    • 2.1 被测对象
    • 2.2 测试类

一、jmockit配置

变量

    
        1.43
    

依赖

		
            org.jmockit
            jmockit
            ${jmockit.version}
            test
        

配置(版本1.42以上必须)

			
                maven-surefire-plugin
                2.21.0 
                
                    
                        -javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
                    
                    
                
            

二、实例

2.1 被测对象

package com.test.service;

/**
 * @author lu
 * @date 2018/12/13
 */
public class JMockitoSupportService {

    private String privateData="privateData";

    private String constuctData;

    private static String staticData;

    {
        constuctData="constuctData";
    }

    static{
        staticData="staticData";
    }

    public boolean sayHello(String output){
        System.out.println(output);
        if(output.equals("true")){
            return true;
        }else{
            return false;
        }
    }

    public static boolean sayStaticHello(String output){
        System.out.println(output);
        if(output.equals("true")){
            return true;
        }else{
            return false;
        }
    }

    public final boolean sayFinalHello(String output){
        System.out.println(output);
        if(output.equals("true")){
            return true;
        }else{
            return false;
        }
    }


    public String getPrivateData() {
        return privateData;
    }

    public String getConstuctData() {
        return constuctData;
    }

    public static String getStaticData() {
        return staticData;
    }
}

2.2 测试类

import com.test.service.JMockitoSupportService;
import mockit.*;
import mockit.internal.reflection.FieldReflection;
import org.junit.Before;
import org.junit.Test;


public class JMockitNewTest {

	//私有成员mock
    @Injectable
    String privateData="private Mock Data";

	//代码块mock
    @Injectable
    String constuctData="constuct Mock Data";

    @Injectable
    public static String staticData="static Mock Data";
    
	//测试对象
    @Tested
    JMockitoSupportService jMockitoSupportService;

    public static class JMockitNewMockTest extends MockUp{

		//mock代码块和构造函数
        @Mock
        void $init(Invocation invocation) {
            JMockitoSupportService jMockitoSupportService=invocation.getInvokedInstance();
            //1.44 废弃 1.45 删除
            Deencapsulation.setField(jMockitoSupportService,"staticData",staticData);
        }
        //mock静态代码块
        @Mock
        void $clinit(Invocation invocation){

        }
    }


    @Before
    public void setUp() {
        new JMockitNewMockTest();
        //1.45静态变量需要通过反射注入
//        FieldReflection.setFieldValue(JMockitoSupportService.class.getDeclaredField("staticData")
//                ,jMockitoSupportService,staticData);
    }

    @Test
    public void testJMockit(){
        System.out.println(jMockitoSupportService.getConstuctData());
        System.out.println(jMockitoSupportService.getPrivateData());
        System.out.println(jMockitoSupportService.getStaticData());
        System.out.println(JMockitoSupportService.getStaticData());
    }

}

你可能感兴趣的:(工作笔记)