简体   繁体   English

测试Java程序的最佳方法

[英]Best way to test Java Program

I wanted to test the below java program. 我想测试以下Java程序。

package com.arrays;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Find all pairs in an array of non-negative unique numbers whose sum is equal to k
 * For Example. {1,3,4,5,6} and k=9, then {3,6} and {4,5}
 *
 */
public class FindAllPairsIfSumToK {

    public List<Pair> findAllPairsWhoseSumIsK(int[] inputArray, int k) {

        Set<Integer> tempSet = new HashSet<>();
        List<Pair> pairs = new ArrayList<>();

        for(int i=0;i<inputArray.length;i++) {
            tempSet.add(inputArray[i]);
        }

        for(int i=0;i<inputArray.length;i++) {
            if((2*inputArray[i] != k) && tempSet.contains(k-inputArray[i])) {
                pairs.add(new Pair(inputArray[i], k-inputArray[i]));
            }
        }

        return pairs;
    }

    public static void main(String[] args) {

        FindAllPairsIfSumToK findAllPairsIfSumToK = new FindAllPairsIfSumToK();

        //Test 1
        int[] inputArray1 = {1, 3, 5, 7, 9, 11};
        List<Pair> output1 = findAllPairsIfSumToK.findAllPairsWhoseSumIsK(inputArray1, 10);
        assert (output1.size() == 2) ;

        //Test 2
        int[] inputArray2 = {1, 2, 5, 6, 12, 15, 16};
        List<Pair> output2 = findAllPairsIfSumToK.findAllPairsWhoseSumIsK(inputArray2, 17);
        assert (output2.size() == 3) ;
    }

    class Pair{
        int value1,value2;

        public Pair(int value1, int value2) {
            this.value1 = value1;
            this.value2 = value2;
        }
    }
}

This is how I'm trying to test the program 这就是我试图测试程序的方式

//Test 1
            int[] inputArray1 = {1, 3, 5, 7, 9, 11};
            List<Pair> output1 = findAllPairsIfSumToK.findAllPairsWhoseSumIsK(inputArray1, 10);
            assert (output1.size() == 2) ;

I Googled it, But most of them are telling about testing the web application. 我用Google搜索了它,但是大多数人都在谈论测试Web应用程序。 Is it a proper way to test the program? 这是测试程序的正确方法吗? Or Could you please guide? 还是请您指导?

For java there is a Unit Testing Library called JUnit. 对于Java,有一个称为JUnit的单元测试库。 Depending on your IDE, you will have to include it in a different way, but here are the basics. 根据您的IDE,您将不得不以其他方式包括它,但这是基础知识。 If using JUnit, you can write multiple test cases, which is preferred to having the test case in the main function, since you can organise the tests better. 如果使用JUnit,则可以编写多个测试用例,这比在主函数中包含测试用例更可取,因为这样可以更好地组织测试。

If you are using eclipse, to generate a new JUnit TestCase simply right click on the source folder and select: new -> other -> JUnitTestCase and then select a JUnit version. 如果您使用的是eclipse,则只需右键单击源文件夹并选择:new-> other-> JUnitTestCase来生成一个新的JUnit TestCase,然后选择一个JUnit版本。

In a JUnitTestCase all tests are functions annotated with @Test and the results are checked with assertEqals, assertArrayEquals, assertNonNull, etc. If you create a new source folder (call it test or something similar) and create the the same package structure as in your program and create the tests in the same package as the class to be tested, you can access package-private and protected members, but the tests are still organized separately from the main program. 在JUnitTestCase中,所有测试都是使用@Test注释的函数,并使用assertEqals,assertArrayEquals,assertNonNull等检查结果。如果您创建了新的源文件夹(称为test或类似名称),并创建了与您相同的包结构程序并在与要测试的类相同的程序包中创建测试,则可以访问程序包私有和受保护的成员,但是这些测试仍与主程序分开组织。

Your test would look like this using JUnit: 使用JUnit进行的测试如下所示:

@Test
public void Test_FindAllPairsIfSumToK(){
    FindAllPairsIfSumToK findAllPairsIfSumToK = new FindAllPairsIfSumToK();
    int[] inputArray1 = {1, 3, 5, 7, 9, 11};
    List<Pair> output1 = findAllPairsIfSumToK.findAllPairsWhoseSumIsK(inputArray1, 10);
    assertEquals (2, output1.size());
    //maybe check if the correct ones are output:
    output1.foreach(p -> assertEquals(10, p.value1 + p.value2));
}

Also you might want to declare the variables in pair as public final int , so that they can be accessed from outside the package and cannot be changed after the Pair object was constructed. 另外,您可能希望将pair中的变量声明为public final int ,以便可以从包外部访问它们,并且在创建Pair对象后不能更改它们。

This example can be test by running your program with -ea or -enableassertions switch to java. 可以通过使用-ea或-enableassertions切换到java来运行程序来测试此示例。

Only thing that need to be changed is the conditions from == to != since assert is working on the opposite to condition with throwing an exception. 唯一需要更改的是条件==到!=,因为assert在与引发异常的条件相反的条件下工作。

Please check: question 请检查: 问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM