This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Mocking Method call within a Method?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

Im fairy new with mockito and am trying to write a simple unit test for a rest controller.

However im stuck on one particualr bit. The controller gets an instance of a static class i.e



So what I did in my unit test is return a mock that object as such



However my problem is that the getLicenseAuthentication() method of my mock object calls another private method with throws an exception in my unit test .



The exception is thrown because the attribute strategyName is null. This attribute is only set by event listeners when the application fires up in tomcat.

I did not write this code so will be refactoring the way it works using spring security and DI.

But in the meantime is there a way in mockito that I can get it to sort of mock the checkStrategyName method?
 
Sheriff
Posts: 17702
301
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems to me you're going to have to refactor that line of code in the first snippet first. IMO, there's no point in trying to find a workaround for this; just bite the bullet and refactor first.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you have already created a mock for LicenseAuthentication, why don't you mock the call to the getLicenseAuthentication method?

Surely you would want to do this anyway as LicenseAuthentication is not the class under test.
 
Darren Carter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Thank you ever so much for the replies! In the end I started to refactor the first snippet and wow.. I opened a can of worms. There was so much code to refactor Ive spent the best part of two days doing it and nearly completed. The whole application object model is so tightly coupled its been a real pain sorting it out to use DI. Thanks for your help though!



 
Junilu Lacar
Sheriff
Posts: 17702
301
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darren Carr wrote:In the end I started to refactor the first snippet and wow.. I opened a can of worms. There was so much code to refactor Ive spent the best part of two days doing it and nearly completed.


Consider yourself lucky they were just worms and it only took you two days to clean it up! Not that I have anything against worms but I have seen things much more disgusting than that in my day
 
Darren Carter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He he thanks Junilu :-)
 
Darren Carter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys,

After all that refactoring and simlier to this thread, I have another issue that Im not understanding and was hoping someone could help? I think this is die to the first time of using spring-mvc-test

I now have my controller that has three @Autowire attributes



And my unit test



The problem is that I get to the controller ok but the licenseSecurityContect is null in the controller




How can I mock the licenseSecurityContextHelper object? Do I need to use the @ContextConfiguation in my unit test ?

 
Darren Carter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I resolved the problem and posting the solution here to help others .

First off my problem was that I was using the standaloneSetup method from the MockMVCBuilders class. This does not use the configuration that I loaded from the @ContextConfiguation annotation (i.e RegistrationControllerTesting-context.xml)

So it was simply a case of using the MockMVCBuilders.xmlConfigSetup to load in my servlet context with mock objects, and then remove the @ContextConfiguration.

So the code:

This example uses spring-test-mvc and loads in the servlet context from an xml file and injects mocked objects into the controllers @Autowired attributes. It calls a rest service passing in some Json.

RegistrationControllerTesting-context.xml



Unit Test Case



Controller



Hope it helps others.
 
My honeysuckle is blooming this year! Now to fertilize this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic