• Junit测试private方法


    1. package com.bill99.junit;  
    2.   
    3. public class ACase {  
    4.   
    5.     private String echoRequest(String request) {  
    6.         return "Hello!"+request;  
    7.     }  
    8.       
    9.     private String echoRequest() {  
    10.         return "Hello!";  
    11.     }  
    12. }  
    13.  
     
    1. package com.bill99.junit;  
    2.   
    3.   
    4. import java.lang.reflect.Method;  
    5.   
    6. import junit.framework.Assert;  
    7.   
    8. import org.junit.Before;  
    9. import org.junit.Test;  
    10.   
    11. public class ACaseTest {  
    12.   
    13.     ACase a =null;  
    14.       
    15.     @Before  
    16.     public void setUp() throws Exception {  
    17.         a = new ACase();  
    18.     }  
    19.   
    20.     @Test  
    21.     public void testNoParamEchoRequest() throws Exception {  
    22.         //测试没有参数的echoRequest()方法  
    23.         Method testNoParamMethod = a.getClass().getDeclaredMethod("echoRequest", null);   
    24.         //Method对象继承自java.lang.reflect.AccessibleObject,父类方法setAccessible可调  
    25.         //将此对象的 accessible 标志设置为指示的布尔值。值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。值为 false 则指示反射的对象应该实施 Java 语言访问检查。                                            
    26.         //要访问私有方法必须将accessible设置为true,否则抛java.lang.IllegalAccessException  
    27.         testNoParamMethod.setAccessible(true);   
    28.         //调用  
    29.         Object result = testNoParamMethod.invoke(a, null);  
    30.         System.out.println(result);  
    31.         Assert.assertNotNull(result);  
    32.           
    33.     }  
    34.       
    35.     @Test  
    36.     public void testParamEchoRequest() throws Exception {  
    37.         //测试带有参数的echoRequest(String request)方法  
    38.         Method testNoParamMethod = a.getClass().getDeclaredMethod("echoRequest",String.class);   
    39.         testNoParamMethod.setAccessible(true);   
    40.         //调用  
    41.         Object result = testNoParamMethod.invoke(a, "this is a test information");  
    42.         System.out.println(result);  
    43.         Assert.assertNotNull(result);  
    44.           
    45.     }  
    46.   
    47. }  

    https://blog.csdn.net/iameyama/article/details/50411212

    IDEA配置junit:

    http://www.360doc.com/content/17/0701/11/10072361_667938060.shtml

  • 相关阅读:
    U盘启动盘的制作与U盘重装系统
    如何使用鲁大师进行驱动备份
    电子科大POJ "3*3矩阵的乘法"
    数字图像处理之sobel边缘检测
    (续)一个demo弄清楚位图在内存中的存储结构
    VC++6.0出现no compile tool is associated with the extension.解决方法
    显卡参数简单介绍
    数字图像处理之位图在计算机中的存储结构
    图像处理之边缘检测概述
    linux下mysql数据库的操作
  • 原文地址:https://www.cnblogs.com/twoheads/p/9543926.html
Copyright © 2020-2023  润新知