数组测试 --Junit

1、添加依赖

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.1'
    testCompile 'junit:junit:4.12'
}

2.java代码

package com.example.testforgit;

/**
 * Created by ** on 2017/3/13.
 */

public class Work {
    public static int forsum(int[] array){
        if(array.length==0 || array==null){
            return 0;
        }
        int Sum1 = 0;//存储当前连续n项的和
        int max =0;//存储连续子元素和的最大值
        for(int i=0;i<array.length;i++){
            if(Sum1<=0){
                Sum1=array[i];
            }else{
                Sum1+=array[i];
            }
            if(Sum1>max){
                max=Sum1;
            }
        }
        return max;
    }
}

3.测试部分

package com.example.testforgit;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 * Created by 金林 on 2017/3/13.
 */
public class WorkTest {
    private Work mwork;
    @Before
    public void setUp() throws Exception {
            mwork = new Work();
    }

    @Test
    public void forsum() throws Exception {
        int[] array={-1,2,3,-4};
        int result = 5;
        result=mwork.forsum(array);
        System.out.print(result);

    }


}

原文地址:https://www.cnblogs.com/jinlindb/p/6567015.html