What’s the difference between Mockito Matchers isA, any, eq, and same?

any() checks absolutely nothing. Since Mockito 2.0, any(T.class) shares isA semantics to mean “any T” or properly “any instance of type T“. This is a change compared to Mockito 1.x, where any(T.class) checked absolutely nothing but saved a cast prior to Java 8: “Any kind object, not necessary of the given class. The class argument … Read more

How to verify static void method has been called with power mockito

If you are mocking the behavior (with something like doNothing()) there should really be no need to call to verify*(). That said, here’s my stab at re-writing your test method: @PrepareForTest({InternalUtils.class}) @RunWith(PowerMockRunner.class) public class InternalServiceTest { //Note the renaming of the test class. public void testProcessOrder() { //Variables InternalService is = new InternalService(); Order order … Read more

How to mock method e in Log

This worked out for me. I’m only using JUnit and I was able to mock up the Log class without any third party lib very easy. Just create a file Log.java inside app/src/test/java/android/util with contents: package android.util; public class Log { public static int d(String tag, String msg) { System.out.println(“DEBUG: ” + tag + “: … Read more

Integration tests for AspectJ

Let us use the same sample code as in my answer to the related AspectJ unit testing question: Java class to be targeted by aspect: package de.scrum_master.app; public class Application { public void doSomething(int number) { System.out.println(“Doing something with number ” + number); } } Aspect under test: package de.scrum_master.aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import … Read more

PowerMock, mock a static method, THEN call real methods on all other statics

What are you looking for is called partial mocking. In PowerMock you can use mockStaticPartial method. In PowerMockito you can use stubbing, which will stub only the method defined and leave other unchanged: PowerMockito.stub(PowerMockito.method(StaticUtilClass.class, “someStaticMethod”)).toReturn(5); also don’t forget about the @PrepareForTest(StaticUtilClass.class)

Mocking Logger and LoggerFactory with PowerMock and Mockito

EDIT 2020-09-21: Since 3.4.0, Mockito supports mocking static methods, API is still incubating and is likely to change, in particular around stubbing and verification. It requires the mockito-inline artifact. And you don’t need to prepare the test or use any specific runner. All you need to do is : @Test public void name() { try … Read more