EvoSuite / evosuite

EvoSuite - automated generation of JUnit test suites for Java classes
http://www.evosuite.org
GNU Lesser General Public License v3.0
824 stars 341 forks source link

-Dtarget_method flag throws NPE #477

Open elizabethdinella opened 1 month ago

elizabethdinella commented 1 month ago

Context

-Dtarget_method flag results in a NPE.

java -jar evosuite-1.2.0.jar -class Foo.Foo -projectCP foo.jar -Dtarget_method="getBitLength()I"
* EvoSuite 1.2.0
* Going to generate test cases for class: Foo.Foo
* Starting Client-0
* Connecting to master process on port 7958
* Analyzing classpath:
  - foo.jar
* Finished analyzing classpath
* Generating tests for class Foo.Foo
* Test criteria:
  - Line Coverage
  - Branch Coverage
  - Exception
  - Mutation testing (weak)
  - Method-Output Coverage
  - Top-Level Method Coverage
  - No-Exception Top-Level Method Coverage
  - Context Branch Coverage
[Progress:>                             0%] [Cov:>                                  0%]* Total number of test goals for DYNAMOSA: 426
* Using seed 1720468146141
* Starting evolution
[MASTER] 15:49:06.856 [logback-2] ERROR ClientNodeImpl - Error when generating tests for: Foo.Foo with seed 1720468146141. Configuration id : null
java.lang.NullPointerException: null
    at org.evosuite.ga.metaheuristics.mosa.structural.MultiCriteriaManager.addDependencies4WeakMutation(MultiCriteriaManager.java:298)
    at org.evosuite.ga.metaheuristics.mosa.structural.MultiCriteriaManager.<init>(MultiCriteriaManager.java:121)
    at org.evosuite.ga.metaheuristics.mosa.DynaMOSA.generateSolution(DynaMOSA.java:146)
    at org.evosuite.ga.metaheuristics.TestSuiteAdapter.generateSolution(TestSuiteAdapter.java:136)
    at org.evosuite.strategy.MOSuiteStrategy.generateTests(MOSuiteStrategy.java:122)
    at org.evosuite.TestSuiteGenerator.generateTests(TestSuiteGenerator.java:630)
    at org.evosuite.TestSuiteGenerator.generateTestSuite(TestSuiteGenerator.java:208)
    at org.evosuite.rmi.service.ClientNodeImpl.lambda$startNewSearch$0(ClientNodeImpl.java:140)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
* Computation finished
[MASTER] 15:49:06.962 [main] ERROR SearchStatistics - No statistics has been saved because EvoSuite failed to generate any test case
[MASTER] 15:49:07.063 [main] ERROR TestGeneration - failed to write statistics data

For the following Java file:

package Foo;

public class Foo {

    public int m_sign;

    public int[] m_magnitude;

    public int m_numBits = -1;

    public int m_numBitLength = -1;

    public Foo() { }

    public int calcBitLength(int indx, int[] mag) {
        for (; ; ) {
            if (indx >= mag.length)
                return 0;
            if (mag[indx] != 0)
                break;
            ++indx;
        }
        int bitLength = 32 * ((mag.length - indx) - 1);
        int firstMag = mag[indx];
        bitLength += BitLen(firstMag);
        if (m_sign < 0 && ((firstMag & -firstMag) == firstMag)) {
            do {
                if (++indx >= mag.length) {
                    --bitLength;
                    break;
                }
            } while (mag[indx] == 0);
        }
        return bitLength;
    }

    public int getBitLength() {
        if (m_numBitLength == -1) {
            m_numBitLength = m_sign == 0 ? 0 : calcBitLength(0, m_magnitude);
        }
        return m_numBitLength;
    }

    public static int BitLen(int w) {
        return (w < 1 << 15 ? (w < 1 << 7 ? (w < 1 << 3 ? (w < 1 << 1 ? (w < 1 << 0 ? (w < 0 ? 32 : 0) : 1) : (w < 1 << 2 ? 2 : 3)) : (w < 1 << 5 ? (w < 1 << 4 ? 4 : 5) : (w < 1 << 6 ? 6 : 7))) : (w < 1 << 11 ? (w < 1 << 9 ? (w < 1 << 8 ? 8 : 9) : (w < 1 << 10 ? 10 : 11)) : (w < 1 << 13 ? (w < 1 << 12 ? 12 : 13) : (w < 1 << 14 ? 14 : 15)))) : (w < 1 << 23 ? (w < 1 << 19 ? (w < 1 << 17 ? (w < 1 << 16 ? 16 : 17) : (w < 1 << 18 ? 18 : 19)) : (w < 1 << 21 ? (w < 1 << 20 ? 20 : 21) : (w < 1 << 22 ? 22 : 23))) : (w < 1 << 27 ? (w < 1 << 25 ? (w < 1 << 24 ? 24 : 25) : (w < 1 << 26 ? 26 : 27)) : (w < 1 << 29 ? (w < 1 << 28 ? 28 : 29) : (w < 1 << 30 ? 30 : 31)))));
    }
}

Evosuite can successfully generate tests for the above program when I do not use the -Dtarget_method flag. Interestingly, it also successfully runs when I comment on the body of methods BitLen and calcBitLength.

EvoSuite Arguments

Please provide the whole EvoSuite commmand you executed (if relevant) java -jar $PRECOND_LIBS/evosuite-1.2.0.jar -class Foo.Foo -projectCP foo.jar -Dtarget_method="getBitLength()I"

Additional info

I am using Java 8 and evosuite 1.2.0

java -version
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-b10)
OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)

target_method_issue.tar.gz

I've attached a tar file with the java file and run command.