squaretest

Open full view…

Template changes to get setup constructor call to use variables not literals

ltalley
Sat, 07 Sep 2019 01:06:05 GMT

I'd like to understand how to modify a template to get the generated test code to make and use local variables for the members of the class under test, and use those variables in the calls to the constructor. Example: //class to be tested public class Bob { private Short year; } //currently generated test public class BobTest { private Bob bobUnderTest; @Before public void setUp() { bobUnderTest = new Bob(0); } } //What I want Squaretest to generate public class BobTest { private Bob bobUnderTest; private Short year; @Before public void setUp() { bobUnderTest = new Bob(year) } }

nate-squaretest
Fri, 13 Sep 2019 02:59:46 GMT

Hi! The best solution is to modify the setDependencyNamesAndInitExpressions() macro to store non-mocked dependencies in member fields. To do this, change the following lines: --- #elseif(!$dependency.shouldBeMocked) #set($dependency.shouldStoreInReference = false) #end --- to: --- #elseif(!$dependency.shouldBeMocked) #set($dependency.shouldStoreInReference = true) #end --- These are lines 612-614 in the JUnit4 Java template.

ltalley
Thu, 19 Sep 2019 00:36:30 GMT

Bingo, thanks, this is just what I was looking for!