1

我在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.class

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.

有人可以帮我解决这个问题吗,我被困在这里很长时间了,尝试了不同的东西,但没有成功。

4

1 回答 1

0

尝试这样的事情它有效

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
于 2021-05-17T12:46:07.770 回答