-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Comments
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... |
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 |
There are quite a few methods marked as final so I'm reviewing through the implications. |
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? |
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. |
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 |
Works as expected! Thank you! |
Good, thanks for reporting this to help makes things better. |
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?
The text was updated successfully, but these errors were encountered: