Do you know what’s worse that static methods or classes marked as final? I’ll tell you what’s worse: static methods that return final classes. That only provides private constructors.
Here I was, merrily testing my way through a piece of software that sends emails. According to the Java Mail documentation, you are supposed to first create an email session as follows:
Session mailSession = Session.getInstance(properties);
(it is worth noting that all what the getInstance() method does is call the private constructor for Session: new Session(props, null)).
Had Session been a more normal class, I could have mocked it like that:
Session mockSession = mock(Session.class);
and probably checked how it is being passed around like this:
verify(someService).startEmailSession(mockSession);
What follows is what I have to do instead. Continue reading →