Testing handling of static class methods

2015-12-18 Thread Joe Skora
For unit testing, one problem I've run into is overriding the returns from static class methods. For instance, PutJMS contains this code: try { > wrappedProducer = JmsFactory.createMessageProducer(context, true); > logger.info("Connected to JMS server {}", > new Object[]{conte

Re: Testing handling of static class methods

2015-12-18 Thread Matt Burgess
You could move the one static call into an instance method of PutJMS, and use Mockito.spy() to get a partial mock of the processor, then use when() to override the instance method in the test. Not sure if that's how it's done in other places but it's worked for me in the past. Regards, Matt Se

Re: Testing handling of static class methods

2015-12-18 Thread Bryan Bende
If you get it into a protected instance method, you can also make an inner class in your test, something like TestablePutJMS extends PutJMS, and overrides that method to return a mock or whatever you want. That is a common pattern in a lot of the processor tests. On Fri, Dec 18, 2015 at 3:44 PM, M

Re: Testing handling of static class methods

2015-12-18 Thread Joe Skora
Wrapping createMessageProducer() in an instance method is a good suggestion, but it seems overkill just to enable testing. Prompted by Oleg's suggestion, I got around instance variable visibility with Reflection, which is nice because it doesn't require "private" be changed to "protected" in the c

Re: Testing handling of static class methods

2015-12-18 Thread Oleg Zhurakousky
Personally I am with Joe on this one Exposing visibility on the method just for testing is very dangerous as it breaks encapsulation. There are different expectations and consideration on things that are made private, protected and public. Yes, all of that is meaningless when one uses reflectio

Re: Testing handling of static class methods

2015-12-18 Thread Matthew Burgess
Definitely a topic ripe for debate :) In my view there’s a whole spectrum, with one side being the case Oleg describes, where the existing encapsulation is not compromised solely for the sake of testing. On the far side is pure design-by-contract. For example, the case could be made that the JMS

Re: Testing handling of static class methods

2015-12-18 Thread Sean Busbey
You could use PowerMock to either call private static methods directly or to mock them out. https://github.com/jayway/powermock/wiki/MockStatic https://github.com/jayway/powermock/wiki/MockPrivate https://github.com/jayway/powermock/wiki/BypassEncapsulation#invoking-a-private-method On Fri, Dec