• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mocking Method call within a Method?

 
Darren Carter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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?
 
Junilu Lacar
Bartender
Posts: 7486
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • 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
  • 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
Bartender
Posts: 7486
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
He he thanks Junilu :-)
 
Darren Carter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic