简体   繁体   中英

Mocking a void method from the same class that is being tested

I am trying to test a method using mockito and powermockito, this method internally calling a method which returns void, i wanted to mock this method since i dont care about the result of that method. i have been trying on this but unable to find the solution please help me on this.

I tries using powermockito as

SearchProcedureCodeControllerBean mock = mock(SearchProcedureCodeControllerBean.class);
PowerMockito.doNothing().when(mock).setErrorMessage((String)anyObject(), Mockito.anyList().toArray(), (String)anyObject(), (String)anyObject());

setErrorMessage is the void method, signature for this method is as follows

public void setErrorMessage(String errorName, Object[] arguments,
            String messageBundle, String componentId) {
}

You can stub method of a mocked object only. Tested object is definitely not mocked. With Power Mockito you can stub static methods, it's not your case. If you don't care about the method behavior just leave it like this. It`s void so it will baisically don't matter. If it does and you would like to suppress it's influence, it would mean the real problem is with the design as the class under test owns too much responsibility. Extract the method to another class and then mock it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM