aspose file tools*
The moose likes Testing and the fly likes Mocking Method call within a Method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Mocking Method call within a Method?" Watch "Mocking Method call within a Method?" New topic
Author

Mocking Method call within a Method?

Darren Carter
Ranch Hand

Joined: Jul 08, 2010
Posts: 30
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

Joined: Feb 26, 2001
Posts: 5288
    
  10

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1031
    
    5

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

Joined: Jul 08, 2010
Posts: 30
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

Joined: Feb 26, 2001
Posts: 5288
    
  10

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

Joined: Jul 08, 2010
Posts: 30
He he thanks Junilu :-)
Darren Carter
Ranch Hand

Joined: Jul 08, 2010
Posts: 30
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

Joined: Jul 08, 2010
Posts: 30
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mocking Method call within a Method?