-1

I have a Mainclass that I need to test which is dependent on other class. Now I am creating a mock for that class How to test void methods using easymock

MainClass{

  mainClassMethod(){
    dependencyClass.returnVoidMethod();
    //other code
  }
}

TestClass{

    @Before
    setUpMethod(){
        DependencyClass dependencyClassMock = EasyMock.createMock(DependencyClass.class);
    }

    @Test
    testMainClassMethod(){
        EasyMock.expect(dependencyClassMock.returnVoidMethod()).andRetur //this is not working
        dependencyClassMock.returnVoidMethod();
        EasyMock.expectLastCall().anyTimes(); //If I use this, it is invoking the method.

    }
}
//My dependency class code
DependencyClass implements ApplicationContextAware{
    private static ApplicationContext applicationContext;
    private static final String AUTHENTICATION_MANAGER = "authenticationManagers";

    returnVoidMethod(){
        ProviderManager pm = (ProviderManager) getApplicationContext().getBean(AUTHENTICATION_MANAGER); //this is returning null

    }
     //othercode
     //getters and setters of application context
}
CSK
  • 83
  • 1
  • 10

2 Answers2

0

As described in the Easymock Documentation you don't put the method inside an expect() (since there is no return). You can just call the mocked method by itself and if it is in "record" mode then it is implied an expect.

dependencyClassMock.returnVoidMethod();

If you need to throw an Exception or say the method can be called anyTimes() you can use expectLastCall()

@Test
public void testMainClassMethod(){

    dependencyClassMock.returnVoidMethod();
    EasyMock.expectLastCall().anyTimes();

    ...
     //later to replay the mock
    EasyMock.replay(dependencyClassMock);

    //now this method is actually called
    dependencyClassMock.returnVoidMethod();

}

EDIT : Just noticed that you don't have the dependencyClassMock as field:

public class TestClass{
    DependencyClass dependencyClassMock

    @Before
    setUpMethod(){
        dependencyClassMock = EasyMock.createMock(DependencyClass.class);
    }

...//rest of class is as described above
dkatzel
  • 31,188
  • 3
  • 63
  • 67
0

@dkatzel the test is completely wrong..... you are calling manually a method and the you verify if it was called...of course it was! ...that's not the right way (my opinion)

A better way (my opinion) would be to extend the mehod class you would like to test, override that method and in the body just put a boolean variable as a flag to k now if the method was called or not....you don't even need to use EasyMock

Example

Class DependencyClass {
 public void returnVoidMethod() {
  [.... content ...]
 }
} 

Class A_test {

 @Test
 public void checkVoidMethodCalled() {
  A_mod obj = new A_mod();
  mainClassMethod();
  assertTrue(obj.called);
 }

Class A_mod extends DependencyClass {
 boolean called = false;

 @Override
 public void returnVoidMethod() {
  called = true;
 }
}


}

You are welcome.

shaolin
  • 463
  • 5
  • 13