my dog learned polymorphism
The moose likes Testing and the fly likes Suppressing a method using Powermock results in AssertionError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Suppressing a method using Powermock results in AssertionError" Watch "Suppressing a method using Powermock results in AssertionError" New topic

Suppressing a method using Powermock results in AssertionError

Mani Shah

Joined: Jan 16, 2014
Posts: 4
Hi ,

I was trying to suppress a method of different class and I am getting following error:

Unexpected method call Class1.func1():
at org.easymock.internal.MockInvocationHandler.invoke(
at org.easymock.internal.ObjectMethodsFilter.invoke(
at org.easymock.internal.ClassProxyFactory$MockMethodInterceptor.intercept(
at com.mock.Class1$$EnhancerByCGLIB$$443e28a1.func1(<generated>)
at com.mock.Class2.func2(
at com.mock.TestClass2.testfunc2(

Following is the code:

public class Class2 {

public void func2(){

Class1 obj1 = new Class1();
System.out.println("class1 member :" + obj1.member);
int val = obj1.func1();


public class Class1 {

public int member =14;

public int func1() {
return 16;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@PrepareForTest( { Class2.class })
public class TestClass2 {

public void testfunc2(){

Class2 class2 = new Class2();

try {
Class1 mock1 = PowerMock.createMockAndExpectNew(Class1.class);
} catch (Exception e) {
// TODO Auto-generated catch block


Can someone please help here?

Steve Luke

Joined: Jan 28, 2003
Posts: 4181

I don't think you are mixing the supress and replay methods correctly. The suppress doesn't prevent the method call from happening, it just prevents the code from being executed. Then you put the mock in replay mode but don't tell it what methods to expect, so the mock expects no methods to be called. When you run the test a method is called so the assertion that no method is called fails. The solution is to add an expect for the method to be called.

I agree. Here's the link:
subject: Suppressing a method using Powermock results in AssertionError
It's not a secret anymore!