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 18, 2015 at 4:42 PM, Joe Skora <jsk...@gmail.com> wrote: > 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 class under test and doesn't require an inner test > class. But, that doesn't appear to be possible for static methods, so > wrapping with class methods may be the only choice. Hopefully, I've missed > something. > > > On Fri, Dec 18, 2015 at 3:58 PM, Bryan Bende <bbe...@gmail.com> wrote: > >> 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, Matt Burgess <mattyb...@gmail.com> wrote: >> >> > 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 >> > >> > Sent from my iPhone >> > >> > > On Dec 18, 2015, at 3:20 PM, Joe Skora <jsk...@gmail.com> wrote: >> > > >> > > 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[]{context.getProperty(URL).getValue()}); >> > >> } catch (final JMSException e) { >> > >> logger.error("Failed to connect to JMS Server due to {}", new >> > >> Object[]{e}); >> > >> session.transfer(flowFiles, REL_FAILURE); >> > >> context.yield(); >> > >> return; >> > >> } >> > > >> > > where JmsFactory.createmessageProducer call being defined as >> > > >> > > public static WrappedMessageProducer createMessageProducer(... >> > > >> > > which presents a problem since it can't be easily overridden for a unit >> > > test. Exercising the >> > > >> > > How you handle this problem? >> > > >> > > Regards, >> > > Joe >> > >>