Mocking static methods in Java without annotations - java

Are there any libraries for the JVM that allow mocking of static methods WITHOUT needing annotations? I am attempting to build this feature into a Clojure testing framework (Midje). Clojure has very poor, or non-existant annotation support.

Use powermock.It's a great library for mocking. And here is an example that explains how to mock static methods.

Related

is mockito uses reflection api to mock objects

Mockito is used to mock the objects for unit testing. Same can be done using java reflection API. Does this mean Mockito is implemented on reflection API of java ?
No, Mockito doesn't use java.lang.reflect.Proxy, which only works on interfaces. (Even if it did, I'd be wary of logic that says "A can be used for B, that means library L used A for B".)
To allow for more flexible mocking, including mocking of concrete classes, Mockito generates bytecode for its mocks using:
CGLib/ASM/Objenesis for Mockito prior to 2.0 [source]
ByteBuddy for Mockito from 2.0 onwards
DexMaker to create .dex files for Android Dalvik VMs (since 1.9.5)
arbitrary implementations of MockMaker to support other platforms and packages (since 1.9.5)
For what it's worth, the CGLib Proxy class that Mockito used was designed to be a drop-in replacement for java.lang.reflect.Proxy.
Side note: Mockito definitely uses Java's reflection API, such as to refer to the Method object in InvocationOnMock.getMethod. This is probably not what you meant by "used to mock the objects", though.

Can PowerMock be used without annotations?

I'm trying to use PowerMock as a library in another application. Is there a way to use it to mock a static method call without using annotations (I'm in Clojure which doesn't really do annotations)
According to powermock support and this blog, I guess there is no way to avoid annotations in test. I guess we need the #PrepareForTest(StaticClass.class) however. So I believe it is not possible to avoid #PrepareForTest atleast. May be I am wrong, but just thought of sharing what I found.
In fact, it's possible, although the way to final solution is painful.
PowerMock runner just initializes test environment in different classloader, in which the classes specified in PrepareForTest annotation are tweaked by Javassist. So assumed you mimic the work of the classloader and call Javassist by yourself, you can achieve the same effect.
As an example, I utilized PowerMock (internals without annotations) to discover name of method for given method reference. Further info can be found on my blog (in Czech, with working examples). I emphasize such an usage is only experimental and not suitable for production usage.

Mock Object Libraries in Java

Would anyone suggest a Mock library and provide the reasoning behind the pick?
I am looking to introduce one to the existing code base.
Thanks.
This is the best comparison I've seen, of multiple Java mocking frameworks, including EasyMock and mockito. [The original page is offline; this link is to an archived copy.]
It includes EasyMock, Mockito, jMock, SevenMock, JMockit, rMock, Unitils.
I used EasyMock first, but have moved to Mockito. Unit tests created with EasyMock were sometimes fragile and hard to debug. Mockito is easy, partly because it distinguishes between stubbing and verification. Here's a Mockito-oriented comparison of it and EasyMock: http://code.google.com/p/mockito/wiki/MockitoVSEasyMock.
My suggestion is JMockit (I wrote it). The project site has a lot of information (and hundreds of actual JUnit tests) comparing several Java mocking APIs (EasyMock, jMock, JMockit, Mockito, PowerMock, Unitils Mock), as well as an extensive feature comparison matrix.
easymock. Reasons, from their site
'EasyMock provides Mock Objects for interfaces (and objects through the class extension) by generating them on the fly using Java's proxy mechanism. Due to EasyMock's unique style of recording expectations, most refactorings will not affect the Mock Objects. So EasyMock is a perfect fit for Test-Driven Development.'
Here is another comparison: http://softwareinabottle.wordpress.com/2010/09/12/finding-the-mock-framework-that-best-suits-me-part-1-the-contenders/
http://softwareinabottle.wordpress.com/2010/10/06/comparing-java-mock-frameworks-part-2-creating-mock-objects/

Is it possible to mock a Java protocol buffer message?

Protocol buffer classes are marked final, presumably for efficiency; however, this makes them quite difficult to test with -- Mockito can't mock/spy on final classes. I've tried using PowerMockito with no success: I get a ClassFormatError when preparing the final class for the test.
My solution up until now is to create mockable adapter interfaces, but I'm hoping there's a less laborious approach.
JMockit can handle final and static. Just pay attention to how to set it up as it requires the -javaagent JVM parameter, or classpath tweaks, or extra annotations to be able to mock final and static stuffs.
JDave has an Unfinalizer that integrates with the JMock ClassImposteriser
It can't unfinalize classes loaded from the the boot classloader, and it requires a VM argument when launching the tests.

How do I define dynamic and just-in-time bindings with Guice?

I am trying to use Guice for a test framework based on TestNG. This frameworks analyzes the test class for dependencies and provides them eliminating the need to build them in tests.
Guice is all about injection and I think is a good fit for the framework. But the question is how do I define bindings after I have created the injector? This is needed because tests may override bindings to substitute default implementations with mocks.
Besides that, I want to guess the implementation at runtime in some cases based on class names conventions. Sounds like Just-in-type binding feature. But how do I provide my own just-in-time binding provider?
That kind of dynamic behaviour isn't supported out-of-the-box, but you can achieve a lot with module rewriting. Take a look at Guiceberry, which already implements mock-substitution for JUnit tests. (And consider submitting a TestNG patch to them, they'd love that!)

Categories

Resources