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 – java.lang.IllegalStateException: Failed to transform class

Powermock 1.6.3 uses javassist 3.15.2-GA which does not support certain types. Using 3.18.2-GA javassist worked for me. You may want to override dependency in your project. <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.18.2-GA</version> </dependency> You may face another problem for which the solution lies here Mockito + PowerMock LinkageError while mocking system class Hope this helps.

Mockito’s Matcher vs Hamcrest Matcher?

Hamcrest matcher methods return Matcher<T> and Mockito matchers return T. So, for example: org.hamcrest.Matchers.any(Integer.class) returns an instance of org.hamcrest.Matcher<Integer>, and org.mockito.Matchers.any(Integer.class) returns an instance of Integer. That means that you can only use Hamcrest matchers when a Matcher<?> object is expected in the signature – typically, in assertThat calls. When setting up expectations or verifications … 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

Java verify void method calls n times with Mockito

The necessary method is Mockito#verify: public static <T> T verify(T mock, VerificationMode mode) mock is your mocked object and mode is the VerificationMode that describes how the mock should be verified. Possible modes are: verify(mock, times(5)).someMethod(“was called five times”); verify(mock, never()).someMethod(“was never called”); verify(mock, atLeastOnce()).someMethod(“was called at least once”); verify(mock, atLeast(2)).someMethod(“was called at least twice”); … Read more