Auto-generating Unit-Tests for legacy Java-code [closed]
Asked Answered
L

5

13

What is the best, preferably free/open source tool for auto-generating Java unit-tests? I know, the unit-tests cannot really serve the same purpose as normal TDD Unit-Tests which document and drive the design of the system. However auto-generated unit-tests can be useful if you have a huge legacy codebase and want to know whether the changes you are required to make will have unwanted, obscure side-effects.

Labiche answered 17/9, 2008 at 8:35 Comment(0)
D
4

Not free. Not opensource. But I have found AgitarOne Agitator (http://www.agitar.com/solutions/products/agitarone.html) to be REALLY good for automatically generating unit tests AND looking for unwanted obscure side effects

Dangerous answered 17/9, 2008 at 9:37 Comment(1)
I would love to see an open source implementation of something like Agitar - it is the sort of thing that Open Source should be really good for.Dangerous
F
4

It is interesting, but such generated unit tests can actually be useful. If you're working on a legacy application, it will be often hard to write correct, state-of-the-art unit tests.

Such generated tests (if you have a way of generating them of course) can then make sure that behavior of code stays intact during your changes, which then can help you refactor the code and write better tests.

Now about generating itself. I don't know about any magic tool, but you may want to search for JUnit functionality about including some tests in javadocs for methods. This would allow you to write some simple tests. And yes, it's actually of some value.

Second, you can just write "big" tests by hand. Of course, these wouldn't be unit tests per se (no isolation, potential side-effects, etc), but could be good first step. Especially if you have little time and a legacy application.

Bonus Tip! There is an excellent book "Working effectively with legacy code" with examples in Java, including techniques right to use in such situations. Unfortunately you would have to do some things manually, but you would have to do that at some step anyway.

Frogmouth answered 17/9, 2008 at 8:35 Comment(0)
D
4

Not free. Not opensource. But I have found AgitarOne Agitator (http://www.agitar.com/solutions/products/agitarone.html) to be REALLY good for automatically generating unit tests AND looking for unwanted obscure side effects

Dangerous answered 17/9, 2008 at 9:37 Comment(1)
I would love to see an open source implementation of something like Agitar - it is the sort of thing that Open Source should be really good for.Dangerous
K
3

To be honest, I probably wouldn't do this. Unit tests are isolated and you won't actually know if you have "unwanted, obscure side-effects" because everything is walled off from the other things that cause the side effects. As a result, you need integration or system testing and that is not something you can automate.

Build a few high-level, end-to-end system tests which give you a degree of confidence and then use coverage testing to find out what you've missed, The downside is that when bugs crop up, it will be harder to point to their exact cause, but the upside is that you'll be far more likely to see the bugs.

Once you find bugs, write unit tests just for them. As you move forward, you can use TDD for the bits you want to refactor.

I know this probably wasn't the answer you want to hear, but I've been testing for many, many years and this is a solid approach (though I would hardly call it the only approach :)

Ketron answered 17/9, 2008 at 8:43 Comment(1)
as a complement to this approach I recommand reading objectmentor.com/resources/articles/… (warning PDF) and this www-128.ibm.com/developerworks/java/library/j-legacytest.html I don't think you really want to autogenerate the testsHenden
L
3

Coview plugin for Eclipse (http://www.codign.com/products.html) looks just the job. I'm interested in generating tests that cover all the paths in the code, and this seems to do it. It also generates the mocks which should save me tons of time.

Laborious answered 27/2, 2011 at 1:6 Comment(0)
P
2

Diffblue Cover is a product that does this, and there is a free Community Edition that's an IntelliJ plugin, here: https://www.diffblue.com/community-edition/download/

It works by using reinforcement learning to search the space of potentially useful tests, and strives to write human-like tests. It automatically creates mocks and has full Spring/SpringBoot support.

Here's an example test for the owner controller in Spring PetClinic that it wrote:

@Test
public void testInitUpdateOwnerForm() throws Exception {
    // Arrange
    Owner owner = new Owner();
    owner.setLastName("Doe");
    owner.setId(1);
    owner.setCity("Oxford");
    owner.setPetsInternal(new HashSet<Pet>());
    owner.setAddress("42 Main St");
    owner.setFirstName("Jane");
    owner.setTelephone("4105551212");
    when(this.ownerRepository.findById((Integer) any())).thenReturn(owner);
    MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/owners/{ownerId}/edit", 123456789);

    // Act and Assert
    MockMvcBuilders.standaloneSetup(this.ownerController)
        .build()
        .perform(requestBuilder)
        .andExpect(MockMvcResultMatchers.status().isOk())
        .andExpect(MockMvcResultMatchers.model().size(1))
        .andExpect(MockMvcResultMatchers.model().attributeExists("owner"))
        .andExpect(MockMvcResultMatchers.view().name("owners/createOrUpdateOwnerForm"))
        .andExpect(MockMvcResultMatchers.forwardedUrl("owners/createOrUpdateOwnerForm"));
}
Potentiometer answered 21/10, 2020 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.