<p>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.</p>

[{'answer_id': 81201, 'author': 'Ovid', 'author_id': 8003, 'author_profile': 'https://Stackoverflow.com/users/8003', 'pm_score': 2, 'selected': False, 'text': '<p>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 <em>that</em> is not something you can automate.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>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 :)</p>\n'}, {'answer_id': 81502, 'author': 'Vihung', 'author_id': 15452, 'author_profile': 'https://Stackoverflow.com/users/15452', 'pm_score': 3, 'selected': True, 'text': '<p>Not free. Not opensource. But I have found AgitarOne Agitator (<a href="http://www.agitar.com/solutions/products/agitarone.html" rel="nofollow noreferrer">http://www.agitar.com/solutions/products/agitarone.html</a>) to be REALLY good for automatically generating unit tests AND looking for unwanted obscure side effects</p>\n'}, {'answer_id': 82091, 'author': 'Paweł Hajdan', 'author_id': 9403, 'author_profile': 'https://Stackoverflow.com/users/9403', 'pm_score': 2, 'selected': False, 'text': '<p>It is interesting, but such <strong>generated unit tests can actually be useful.</strong> If you\'re working on a legacy application, it will be often hard to write correct, state-of-the-art unit tests.</p>\n\n<p>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 <strong>refactor the code and write better tests</strong>.</p>\n\n<p>Now about <strong>generating itself</strong>. 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.</p>\n\n<p>Second, you can just write "big" tests by hand. Of course, these wouldn\'t be unit tests <em>per se</em> (no isolation, potential side-effects, etc), but could be good first step. Especially if you have little time and a legacy application.</p>\n\n<p><strong>Bonus Tip!</strong> 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.</p>\n'}, {'answer_id': 5130722, 'author': 'robbo', 'author_id': 636046, 'author_profile': 'https://Stackoverflow.com/users/636046', 'pm_score': 2, 'selected': False, 'text': "<p>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. </p>\n"}, {'answer_id': 64465028, 'author': 'the_limey', 'author_id': 3104986, 'author_profile': 'https://Stackoverflow.com/users/3104986', 'pm_score': 2, 'selected': False, 'text': '<p>Diffblue Cover is a product that does this, and there is a free Community Edition that\'s an IntelliJ plugin, here: <a href="https://www.diffblue.com/community-edition/download/" rel="nofollow noreferrer">https://www.diffblue.com/community-edition/download/</a></p>\n<p>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.</p>\n<p>Here\'s an example test for the owner controller in Spring PetClinic that it wrote:</p>\n<pre><code>@Test\npublic void testInitUpdateOwnerForm() throws Exception {\n    // Arrange\n    Owner owner = new Owner();\n    owner.setLastName(&quot;Doe&quot;);\n    owner.setId(1);\n    owner.setCity(&quot;Oxford&quot;);\n    owner.setPetsInternal(new HashSet&lt;Pet&gt;());\n    owner.setAddress(&quot;42 Main St&quot;);\n    owner.setFirstName(&quot;Jane&quot;);\n    owner.setTelephone(&quot;4105551212&quot;);\n    when(this.ownerRepository.findById((Integer) any())).thenReturn(owner);\n    MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get(&quot;/owners/{ownerId}/edit&quot;, 123456789);\n\n    // Act and Assert\n    MockMvcBuilders.standaloneSetup(this.ownerController)\n        .build()\n        .perform(requestBuilder)\n        .andExpect(MockMvcResultMatchers.status().isOk())\n        .andExpect(MockMvcResultMatchers.model().size(1))\n        .andExpect(MockMvcResultMatchers.model().attributeExists(&quot;owner&quot;))\n        .andExpect(MockMvcResultMatchers.view().name(&quot;owners/createOrUpdateOwnerForm&quot;))\n        .andExpect(MockMvcResultMatchers.forwardedUrl(&quot;owners/createOrUpdateOwnerForm&quot;));\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/81160', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4110/']