Automatic generation of unit tests for Java?

JavaUnit Testing

Java Problem Overview


Any tool for automatically generating unit tests for Java code?

UPDATE: Primary intended use is to quickly generate unit tests for legacy code that is going to be refactored. No need to automatically keep the tests in sync with the code after automatic generation.


Almost same question was asked here, but the answer was for .NET instead of Java (Pex was the accepted answer):

https://stackoverflow.com/questions/689087/automatic-generation-of-unit-test-cases-for-net-and-java

Java Solutions


Solution 1 - Java

Commercial tools:

  • AgitarOne seems to be the biggest player in the field. There exists also 30-day trial and free web interface called JUnit Factory. There's an independent review of the tool usage in here, for example. Test generation of AgitarOne heavily focuses on the regression tests.
  • Jtest is another big player, which has been around since 1998. A free trial exists. Both AgitarOne and Jtest have been awarded multiple times, and they are both Eclipse-based IDE:s.
  • SilverMark Test Mentor, Java Edition is a commercial product that apparently can also create tests based on java code. This product I have no experience with.

Free tools:

  • CodePlex AnalytiX used to be a commercial tool (it was the first commercial plugin for Eclipse back in 2001), but since Google bought the company developing it, the product has been released as free. It's a full IDE based on Eclipse and a finalist of Jolt Award in test generation at 2007. The features are nice, but the tool hasn't been updated in long time, so it unfortunately doesn't work with current versions of eclipse, and seems to have problems with code requiring java >= 1.5.
  • EvoSuite is a a winner of SBST (Search-Based Software Testing) '13 unit test generation competition as well as SBST '17 unit test generation competition. There exists an Eclipse plugin, web interface and command line version of the tool. There is a regression criteria documented, but apparently it doesn't work yet - the tests generated target for branch coverage, weak mutation coverage or strong mutation coverage.
  • Randoop uses feedback-directed random test generation approach. The tool has been around since 2007 and has been in continuous development. Randoop can produce both regression tests and error-finding tests. It's a robust and reliable command line tool, and Eclipse plugin exists as well.
  • Palus claims to use both dynamic and static analysis, and is building on top of Randoop. Its authors claim that it has been used internally at Google. My experience with this software is that at least the current version (0.2) seems to have some internal issues and it doesn't seem to scale up well.
  • Daikon, Eclat, Javalanche, Java PathFinder, JCrasher, jWalk, Korat, RecGen and ReCrash are free tools that are available as well.

Note that tests generated by Jtest and AgitarOne both depend on licenced runners and they can't be run if you don't have their plugins integrated. This is not the case for free tools I've tested, which generate independent tests.

Disclaimer: I'm not affiliated with any of these tools but I have done academic research on the subject.

Solution 2 - Java

All three products have a try before you buy option, so you could use them to compare the generated code against a small subset of your application.

Solution 3 - Java

NetBeans does it automatically from Java source files (I guess the other IDEs too).

Maybe you could take a look into the junit plugin (link is currently down :-()?

Solution 4 - Java

Netbeans 8 has a built-in capability to build unit tests that are more than stubs. To use this feature:

  1. Open the class you want to test.
  2. Select Create/Update Tests from the Tools menu.
  3. Select whatever features you want.
  4. Open the created unit test and finish the remaining parts of the test.

Solution 5 - Java

I think Agitar had a tool like this called AgitarOne. I think you can still get it but make sure the company is still there.

Solution 6 - Java

How about using Randoop https://randoop.github.io/randoop/ . This works for both Java and .NET

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionb.rothView Question on Stackoverflow
Solution 1 - JavaeisView Answer on Stackoverflow
Solution 2 - JavaPaul WaglandView Answer on Stackoverflow
Solution 3 - JavaKarussellView Answer on Stackoverflow
Solution 4 - Javajames.garrissView Answer on Stackoverflow
Solution 5 - JavaDror HelperView Answer on Stackoverflow
Solution 6 - Javauser2579911View Answer on Stackoverflow