Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Suppressing a method using Powermock results in AssertionError

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

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

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

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();

System.out.println(val);
}
}


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;

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

@Test
public void testfunc2(){

Class2 class2 = new Class2();

try {
Class1 mock1 = PowerMock.createMockAndExpectNew(Class1.class);
PowerMockito.suppress(PowerMockito.method(Class1.class,"func1"));
PowerMock.replay(mock1,Class1.class);
class2.func2();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

Can someone please help here?

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic