0

I'm writing a Spring Boot application and I want to make sure that I get reasonably well covered with tests.

Rather than writing unit tests it occurred to me that there should be tools available to help with that. Generate the required code stubs.

I'm not talking about generating the actual tests as this would be dangerous, but a tool that would generate test methods and I would then have to add the relevant code. Enough test methods should be generated to cover all branches of a method being tested. For example:

public void myComplicatedMethod(Integer i) {
  if (i++ > 10) {
      invokeOtherMethod(i);
  } else {
      throw new Exception("duh!");
  }
}

Based on that code it is possible to identify a number of tests:

public void testShouldHandleNullI() {
}

public void testShouldInvokeOtherMethodIfILargerThan10() {
}

public void testShouldThrowExceptionIfINotLargerThan10() {
}

I'm thinking it should be possible to identify every path through the method and at the very least generate an equivalent test method name for that path.

I would then go through that code and write all the required tests.

Does anything like that exist either as a command line tool or an IDE extension?

user1283068
  • 1,694
  • 4
  • 15
  • 25
  • Automate where ever you can. I like it. – Vishwa Ratna Dec 18 '19 at 12:58
  • Dont think so much about *code coverage*, think of **requirements coverage**, that is: *does any **desired behavior** have an automated Test?* Unfortunately you cannot measure that so the best way to to reach this goal is *Test Driven Development*. – Timothy Truckle Dec 18 '19 at 17:01
  • Does this answer your question? [Automatic generation of unit tests for Java?](https://stackoverflow.com/questions/2131935/automatic-generation-of-unit-tests-for-java) – jps1984 Dec 18 '19 at 17:01
  • @jps1984 Yes and no. The tools referenced on that page are no longer available or do not work with newer versions of eclipse. I'm looking at EvoSuite right now. I'll post an answer to my question if that is a workable solution. – user1283068 Jan 29 '20 at 13:04
  • @TimothyTruckle I do think about code coverage with respect to unit testing and the relevance to TDD. What you are referring to is acceptance tests where BDD frameworks like Cucumber be an excellent way to document compliance. It is possible to use BDD for unit testing (makes a lot of sense actually) but however you decide to do it, you need a lot of testing for undesired behavior where it would make sense to have stubs auto generated just as a reminder to the developer. – user1283068 Jan 29 '20 at 13:10

0 Answers0