Open full view…

Template changes to accommodate mocking of all local references

Tue, 29 Oct 2019 11:11:24 GMT

How to modify template to mock all local reference calls Example: //Class to be tested public class Foo { public String getAccountBalance( int accountNumber ){ Account account= getAccount(accountNumber); //scenario 1 [method in same class] Account account= Account.getAccount(accountNumber); //scenario 2 [static method in any Class] Boo boo=new Boo(); Account account= boo.getAccount(accountNumber); //scenario 3 [method call on any other local objects] } } When ever a Test method is generated for "getAccountBalance" method, I want to auto generate code to mock all 3 scenarios . Any Help !!!!!

Wed, 30 Oct 2019 14:01:00 GMT

Hi! Squaretest is currently unable to accommodate these cases. For scenario 1: Squaretest does not provide information about the private methods within the source class (Foo) that a given method interacts with; it does not consider these to be dependency interactions. For scenario 2: Squaretest does not provide information about which static methods a source method interacts with; it does not consider these to be dependency interactions. For scenario 3: Squaretest can't mock local variables that are constructed in a given source method; the exception is: you can modify the templates to configure dependencies that a source method interacts with to return mock objects; e.g. if getAccountBalance() calls: this.accountManager.getAccount(accountNumber), you could modify the template to make accountManager return a mock Account object.

Thu, 31 Oct 2019 04:56:03 GMT

Thanks a lot for quick reply. Can you help me what modifications needs to be done for template to accommodate scenario 3. Also Currently All instance variables of type Object are not mocked. How to mock all Instance variables? I really appreciate if you can help !!

Thu, 14 Nov 2019 21:37:24 GMT

Sorry for the delay. The recent update should help with this use case. Squaretest 1.4 was released on 11/11/2019. This likely does what you want with regard to mocking (or providing values for) the dependency interaction return types. Squaretest 1.4 stores the return values for the dependency interactions in local variables in many cases. If the return types are DTO beans, Squaretest also invokes all of the setters on the local variables. If you want to use mocks for non-DTO beans that do not have a constructor, please replace the initializeDependencyInteractionMethodReturnType(...) macro with the macro on https://gist.github.com/nate-dev/604a3d6889368cb85f12d19e000b777f. You can adjust the following if statement values to determine when to mock the dependency interaction return types. --- #if($di.methodCallExpression.type.mockable && $di.method.returnType.canonicalName && $di.method.returnType.initExpression == 'null' && !$di.methodCallExpression.type.dtoBean && !$macro.shouldUseSpy && !$StringUtils.equalsAny($di.methodCal lExpression.type.canonicalName, 'java.util.List', 'java.util.Map')) #set($di.method.returnType.shouldBeMocked = true) #end ---