2024-03-19

Test and Data Generation for Java Unit tests

https://sylvainleroy.com/wp-admin/options-general.php?page=ad-inserter.php#tab-2

Today I was preparing a presentation about Software quality for a TechTalk on Thursday. I made a search on the Internet about Automatic Unit test generators and Generators. I will present some I have tried. Today, we will speak of Randoop.

Randoom Test Generator

Randoom Test Generator

The first tool’s name is Randoop. This tool is existing since 2007 and its purpose is to generate automatically unit tests 🙂 Directly from your class definition!

To use it you have two choices:

  • You can use your software JAR or classpath directory.
  • You can it in your test compile path (on Gradle or maven) and creates a main or unit test.

To explain shortly the theory, thanks to the reflection it’s quite easy to produce automatic tests validating some contracts of your API.

Some examples: – toString() should never return null or throws an Exception – equals() and compareTo() methods have a long list of constraints – Reflexivity: o.equals(o) == true – Symmetry: o1.equals(o2) == o2.equals(o1) – Transitivity: o1.equals(o2) && o2.equals(o3) ⇒ o1.equals(o3) – Equals to null: o.equals(null) == false – It does not throw an exception

Therefore this tool is generating unit tests with JUnit(TestSuite) for the list of classes you provide.

I have done some tests and you can reach 50-60% of coverage quite easily.

The main drawbacks of the solution are: – The unit tests are drawing a snapshot (precise picture) of your and its behavior however some tests are really non-sense and you don’t want to edit them. – They don’t replace handwritten tests since the tool is not understanding the different between a String parameter and fullName. He will mostly use dumb strings.

About the technology, it’s not production-ready: – I had troubles with the jar and its dependency plume. – The JAR is a fat jar and coming with dependencies that broke my software.

In conclusion, I will fork the software and try to fix the problems to make it more popular 🙂

Sylvain Leroy

Senior Software Quality Manager and Solution Architect in Switzerland, I have previously created my own company, Tocea, in Software Quality Assurance. Now I am offering my knowledge and services in a small IT Consulting company : Byoskill and a website www.byoskill.com Currently living in Lausanne (CH)

View all posts by Sylvain Leroy →