如何解决 Randoop 中的 Invisible class 错误?

问题描述

我在 Java 中有一个简单的类,包含类似这样的内容

package math;

public class Math {
    /*Expected Behavior:
          Given upperBound >= 0,the method returns
               1 + 2 + ... + upperBound                 
      But This method is buggy and works only on
      inputs with odd value,e.g. for upperBound == 4,the method returns 1 + 2 + 3 + 4 + 1 instead of
      1 + 2 + 3 + 4                                   */
    public static int sum(int upperBound) {
        int s = 0;
        for (int i = 0; i <= upperBound; i++) {
            s += i;
        }
        if (upperBound % 2 == 0) {// <--------- BUG!
            s++;                  // <--------- BUG!
        }                         // <--------- BUG!
        return s;
    }
}

为此,我正在尝试使用 randoop 生成单元测试用例,下面给出了我在这里使用的命令,我的 Math.class 文件位于 /home/niteshb/Downloads/randoop-4.2.6/Tests 中。班级

java -cp /home/niteshb/Downloads/randoop-4.2.6:/home/niteshb/Downloads/randoop-4.2.6/randoop-all-4.2.6.jar randoop.main.Main gentests --testclass=Math --literals-file=CLASSES

这里执行后我收到错误

Cannot instantiate non-visible Math specified via --testclass or --classlist.

Will try to generate tests for 0 out of 1 classes.
You provided no methods to test,so no tests for them can be generated.

Additional diagnostis appear below.
Model with hashcode 1253946629:
  classtypes = [java.lang.Object]
  inputTypes = []
  coveredClassesGoal = []
  classLiteralMap = {}
  annotatedTestValues = []
  contracts = ContractSet[size=12]
    arity 1: [randoop.contract.EqualsReflexive@7ce6a65d,randoop.contract.EqualsToNullRetFalse@1500955a,randoop.contract.EqualsReturnsnormally@e874448,randoop.contract.ComparetoReflexive@29b5cd00,randoop.contract.SizetoArrayLength@60285225]
    arity 2: [randoop.contract.EqualsSymmetric@7113b13f,randoop.contract.EqualsHashcode@45820e51,randoop.contract.ComparetoAntiSymmetric@42d8062c,randoop.contract.ComparetoEquals@6043cd28]
    arity 3: [randoop.contract.EqualsTransitive@cb51256,randoop.contract.ComparetoSubs@59906517,randoop.contract.ComparetoTransitive@5bfbf16f]
  omitMethods = [
    \bensuresCapacity\b
    ^\Qcom.google.common.collect.Iterators.cycle(
    ^\Qorg.apache.commons.math4.genetics.geneticAlgorithm.getRandomGenerator()\E$
    ^\Qorg.apache.commons.math4.util.FastMath.random()\E$
    ^\Qjava.util.Date.<init>()\E$
    ^\Qorg.joda.time.DateTime.Now()\E$
    ^\Qorg.joda.time.LocalDate.<init>\E$
    ^\Qnew org.joda.time.Partial.<init>()\E$
    ^\Qjava.io.File.list()\E$
    ^\Qjava.io.File.list(java.io.FilenameFilter)\E$
    ^\Qjava.io.File.listFiles()\E$
    ^\Qjava.io.File.listFiles(java.io.FileFilter)\E$
    ^\Qjava.io.File.listFiles(java.io.FilenameFilter)\E$
    ^\Qjava.io.File.listRoots()\E$
    ^\Qjava.lang.class.getSigners()\E$
    ^\Qjava.lang.Object.hashCode()\E$
    ^\Qjava.lang.String.hashCode()\E$
    ^\Qjava.lang.System.clearProperty(java.lang.String)\E$
    ^\Qjava.lang.System.console()\E$
    ^\Qjava.lang.System.currentTimeMillis()\E$
    ^\Qjava.lang.System.getProperties()\E$
    ^\Qjava.lang.System.getProperty(java.lang.String)\E$
    ^\Qjava.lang.System.getProperty(java.lang.String,java.lang.String)\E$
    ^\Qjava.lang.System.getSecurityManager()\E$
    ^\Qjava.lang.System.getenv()\E$
    ^\Qjava.lang.System.getenv(java.lang.String)\E$
    ^\Qjava.lang.System.identityHashCode(java.lang.Object)\E$
    ^\Qjava.lang.system.inheritedChannel()\E$
    ^\Qjava.lang.System.mapLibraryName(java.lang.String)\E$
    ^\Qjava.lang.System.nanoTime()\E$
    ^\Qjava.lang.System.setProperty(java.lang.String,java.lang.String)\E$
    ^\Qjava.lang.reflect.Method.hashCode()\E$
    ^\Qjava.text.BreakIterator.getAvailableLocales()\E$
    ^\Qjava.util.AbstractList.hashCode()\E$
    ^\Qjava.util.AbstractSet.hashCode()\E$
    ^\Qjava.util.Arrays.deepHashCode(java.lang.Object[])\E$
    ^\Qjava.util.Arrays.hashCode(boolean[])\E$
    ^\Qjava.util.Arrays.hashCode(byte[])\E$
    ^\Qjava.util.Arrays.hashCode(char[])\E$
    ^\Qjava.util.Arrays.hashCode(double[])\E$
    ^\Qjava.util.Arrays.hashCode(float[])\E$
    ^\Qjava.util.Arrays.hashCode(int[])\E$
    ^\Qjava.util.Arrays.hashCode(java.lang.Object[])\E$
    ^\Qjava.util.Arrays.hashCode(long[])\E$
    ^\Qjava.util.Arrays.hashCode(short[])\E$
    ^\Qjava.util.Collection.hashCode()\E$
    ^\Qjava.util.Collections.shuffle(java.util.List)\E$
    ^\Qjava.util.Comparator.compare(java.lang.Object,java.lang.Object)\E$
    ^\Qjava.util.List.hashCode()\E$
    ^\Qjava.util.Random.<init>()\E$
    ^\Qjava.util.Set.hashCode()\E$
  ]
Operations: (1)
  java.lang.Object.<init> : () -> java.lang.Object

There are no methods for randoop to test.  See diagnostics above.  Exiting.

有人可以帮助我吗,我被困在这里很长时间尝试了不同的东西,但没有解决..

解决方法

尝试这样的事情它有效

java -classpath /home/niteshb/Downloads/randoop-4.2.6:/home/niteshb/Downloads/randoop-4.2.6/randoop-all-4.2.6.jar randoop.main.Main gentests --testclass=math.Math --output-limit=10