• Junit4参数测试初探


      说来惭愧,最近在面试的时候遇到一个求包含特定字符串的字符串数组的某个最长子项时,没写出来,回来以后研究了一下,并配合写了个单元测试,

    记在这里。

      算法类:

      

     1 /**
     2  * @author violet
     3  */
     4 package com.violet.algorithm;
     5 
     6 public class GetLongestStrItem {
     7     
     8     public String getLongestSubStr(String[] arr,String str){
     9         /**
    10          * 根据给定的字符串,返回字符串数组中包含该字符串的最长子项
    11          */
    12         int max=0,index=-1;
    13         for (int i = 0; i < arr.length; i++) {
    14             if(arr[i].contains(str)){
    15                 if(arr[i].length()>max){
    16                     max=arr[i].length();
    17                     index=i;
    18                 }                
    19             }        
    20         }
    21         if(index!=-1)
    22 //            return "index["+index+"]:"+arr[index]; 
    23             return arr[index];
    24         else
    25             return "unable to find item contains specified str "+str;
    26         
    27     }
    28     
    29     public static void main(String[] args) {
    30         String[] arr={"abc"};
    31         String str="abc";
    32         System.out.println(new GetLongestStrItem().getLongestSubStr(arr, str));;
    33     }
    34 
    35 }

      单元测试类:

      

    /*
     * @author violet
     * @version 21.8.2016
     */
    package junit.tests;
    
    import static org.junit.Assert.*;
    
    import java.util.Arrays;
    import java.util.Collection;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized;
    import org.junit.runners.Parameterized.Parameters;
    
    import com.violet.algorithm.GetLongestStrItem;
    
    //参数化测试
    @RunWith(Parameterized.class)
    
    public class GetLongestStrItemTest {
        GetLongestStrItem getLongestStrItem=new GetLongestStrItem();
    //    成员变量即测试用例需要的传入参数
        private String[] arr;
        private String str;
        private String result;
        
    @Parameters
    
    public static Collection data(){
        String[] arr0={};
        String[] arr1={"abc","abcd","abcdef"};
        return Arrays.asList(new Object[][]{
                {arr0,"ab","unable to find item contains specified str "+"ab"},
                {arr1,"abc","abcdef"}            
        });
    }
    
    //构造方法
    public GetLongestStrItemTest(String[] arr,String str,String result){
        this.arr=arr;
        this.str=str;
        this.result=result;
    }
    
    /**
     * 参数测试
     */
    @Test
    public void testGetLongestSubStr1() {
        String expected=getLongestStrItem.getLongestSubStr(arr,str);
        
        assertEquals(expected, result);        
    }
        /*
         * 多种参数可能,多个用例
         */
    //    @Test
    //    public void testGetLongestSubStr2() {
    //        String[] arr={"abc","abcd","abcdef"};
    //        String str="ab";
    //        String expected=getLongestStrItem.getLongestSubStr(arr, str);
    //        assertEquals(expected, "abcdef");        
    //    }
    
        
    
    }
    View Code
    我不断的寻找
  • 相关阅读:
    黄金现货平台
    现货黄金交易中的黑平台
    删除WIN7系统的共享文件
    delphi 通过控件的handle取得控件
    delphi 句柄
    Delphi EVariantTypeCastError错误的解决方法
    【HTML5】Canvas 实现放大镜效果
    使用CSS3制作导航条和毛玻璃效果
    js原生创建模拟事件和自定义事件的方法
    基于react-native实现的博客园手机客户端强势升级
  • 原文地址:https://www.cnblogs.com/yNds/p/5794611.html
Copyright © 2020-2023  润新知