Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking HystrixCommands #96

Closed
pparth opened this issue Feb 4, 2013 · 8 comments
Closed

Mocking HystrixCommands #96

pparth opened this issue Feb 4, 2013 · 8 comments

Comments

@pparth
Copy link

pparth commented Feb 4, 2013

Hello,

I'm trying to Unit Test a REST resource that uses a Client Access Library of HystrixCommands. Therefore, i need to mock a HystrixCommand and especially its execute() method. Nevertheless, the execute method is marked as "final" and therefore cannot get mocked using EasyMock or Mockito. Is there a solution for this? Do you propose an other way?

@pparth
Copy link
Author

pparth commented Feb 5, 2013

I used Powermock in order to mock the execute() method but stumbled upon an other problem that has to do with another framework i use, namely Dropwizard.

In any case, the HystrixCommand in a Client Access Library for a Service, seems to be the number one prerequisite to be mocked when unit testing using a mocking framework. So, if you marked the method as final, it is apparent that you never had to mock it(?). I'm curious if you have a better approach and do it in an other way or maybe i'm missing something...

@benjchristensen
Copy link
Contributor

I was looking through how our code works (we have 100+ commands in production) and it appears that mocking has always been done at a layer of abstraction above the command or by injecting a different implementation of a command.

I think the choice to make some of the methods final was overly protective and I should remove those so that mocking of a specific command is an option as it is sometimes preferable to injecting a different implementation.

If I just remove the final keyword I believe that should do what you need, correct?

I can't think of any reason not to remove the final keywords nor will it affect anybody currently using the library so it seems harmless

@benjchristensen
Copy link
Contributor

There are quite a few methods marked as final so I'm reviewing through the implications.

@pparth
Copy link
Author

pparth commented Feb 5, 2013

Removing the "final" keyword from execute() will do the trick, i think. Also from queue() method.

What you said about "mocking has always been done at a layer of abstraction above the command" is really interesting! I used to have a service facade wrapped over my Hystrix Commands but i decided to follow your advice about letting the developers use the full power of Commands and providing them with explicit exposure over the special interface!

What kind of wrapping do you perform?

@benjchristensen
Copy link
Contributor

I don't recommend wrapping it just for the sake of wrapping it - for the reasons I gave before. The Hystrix interface is valuable. The "wrapping" occurs naturally through layers of abstraction such as a facade to the next layer of an application. Sorry I didn't make that clear.

The unit testing I was referring to is a layer above where the HystrixCommand is being invoked. Any place we would have used a mocking tool we ended up using injection instead.

I think the right thing is to remove the final methods so you are free to mock whatever methods you wish. I will commit that change shortly.

@benjchristensen
Copy link
Contributor

I have released 1.2.6 with the final modifiers removed so you should be able to mock the commands now.

https://github.com/Netflix/Hystrix/blob/master/CHANGES.md

@pparth
Copy link
Author

pparth commented Feb 6, 2013

Works as expected! Thank you!

@pparth pparth closed this as completed Feb 6, 2013
@benjchristensen
Copy link
Contributor

Good, thanks for reporting this to help makes things better.

abersnaze pushed a commit to abersnaze/Hystrix that referenced this issue Nov 7, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants