EvoSuite / evosuite

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

Property.STRATEGY can end with null using client on same JVM #419

Open saiema opened 2 years ago

saiema commented 2 years ago

https://github.com/EvoSuite/evosuite/blob/8730aedd6cbcbeac1d7f246b4f223bda395212c9/master/src/main/java/org/evosuite/executionmode/TestGeneration.java#L64

In this line, strategy is given a default value (MOSUITE) because it was null. But Properties.STRATEGY is still null. One fix is moving line 61 after the if statement.

This issue appears to spawn from not parsing -Dstrategy=MOSUITE: when given this argument, Evosuite ends up using the default strategy, which coincidentally is MOSUITE.

jose commented 1 year ago

Hi @saiema,

Good catch. And yes, moving Properties.STRATEGY = strategy; to after the if statement would make sense. I would also suggest to adapt the getChosenStrategy to further check the -Dstrategy= property, e.g., replacing

} else if (line.hasOption("generateTests")) {
    strategy = Strategy.ONEBRANCH;
} else if (line.hasOption("generateSuite")) {
    strategy = Strategy.EVOSUITE;
} else if (line.hasOption("generateRandom")) {
    strategy = Strategy.RANDOM;
} else if (line.hasOption("generateNumRandom")) {
    strategy = Strategy.RANDOM_FIXED;
    javaOpts.add("-Dnum_random_tests="
            + line.getOptionValue("generateNumRandom"));
} else if (line.hasOption("generateMOSuite")) {
    strategy = Strategy.MOSUITE;
} else if (line.hasOption("generateSuiteUsingDSE")) {
    strategy = Strategy.DSE;
}

with

} else if (javaOpts.contains("-Dstrategy=" + Strategy.ONEBRANCH.name()) || line.hasOption("generateTests")) {
    strategy = Strategy.ONEBRANCH;
} else if (javaOpts.contains("-Dstrategy=" + Strategy.EVOSUITE.name()) || line.hasOption("generateSuite")) {
    strategy = Strategy.EVOSUITE;
} else if (javaOpts.contains("-Dstrategy=" + Strategy.RANDOM.name()) || line.hasOption("generateRandom")) {
    strategy = Strategy.RANDOM;
} else if (javaOpts.contains("-Dstrategy=" + Strategy.RANDOM_FIXED.name()) || line.hasOption("generateNumRandom")) {
    strategy = Strategy.RANDOM_FIXED;
    javaOpts.add("-Dnum_random_tests="
            + line.getOptionValue("generateNumRandom"));
} else if (javaOpts.contains("-Dstrategy=" + Strategy.MOSUITE.name()) || line.hasOption("generateMOSuite")) {
    strategy = Strategy.MOSUITE;
} else if (javaOpts.contains("-Dstrategy=" + Strategy.DSE.name()) || line.hasOption("generateSuiteUsingDSE")) {
    strategy = Strategy.DSE;
}

@saiema any chance you can pull request these changes?

-- Best, Jose

saiema commented 1 year ago

Dear @jose ,

It's been a while since I last work with Evosuite, my version was heavily modified. I will try to make a pull request with those changes if I can manage some free time to do it.

Kind regards,

Simón.

jose commented 1 year ago

Ok, thanks in advance.

saiema commented 1 year ago

@jose I need to check if my changes didn't introduce any bugs. I think that I only need to run tests that verify arguments, is there any specific set of tests that I can call?

jose commented 1 year ago

I'm not aware of such set of tests. If there isn't any, please write a couple system tests (as part of your pull request).