0

I tried with mockito-inline and it doesn't work in my case.

Class BankService {

    //BankService will autowire 2 repositories, say bankRepo and fundRepo;

    public Fund onMessage(Object obj){
        Processor processor = ProcessorFactory.getProcessor(obj.getAccountType());
        processor.process(obj);
        //rest of logic
    }
}

Class ProcessorFactory {
    public Processor getProcessor(String accountType){
        switch(accountType) {
            case "A": return new AProcessor();
            case "B": return new BProcessor();
            //rest of the logic, simple switch
        }
    }
}

@ExtendsWith(MockitoExtension.class)
Class BankTest {
    
    @Mock
    BankRepository bankRepo;

    @Mock
    Aprocessor mockProcessor;

    @Mock
    FundRepository fundRepo;


    @InjectMock
    BankService bankService;

    @Test
    public void onMessage(){
        when(bankRepo)...
        when(fundRepo)...
        //Now how can I mock ProcessorFactory in bankService?
        try(MockStatic mock = mockStatic(ProcessorFactory.class)) {

           //Strangely the assertThat statement passed, so it means it does return a mockProcessor.
           when(() -> ProcessorFactory.getProcessor(any())).thenReturn(mockProcessor);
           assertThat(ProcessorFactory.getProcessor(any())).isEqualTo(mockProcessor);
           
           //However, when onMessage is called, ProcessorFactory.getProcessor return an actual object, not mockProcessor
           bankService.onMessage(obj);
        }
    }

}

I know junit4 has powermock and prepareForTest and I've done static mocking before in junit4. However for this project, I can only use junit5, and I can't change the code.

If I don't have a mock object returned from ProcessorFactory, it will be very difficult to test because these processors calls many other services and db operations. I know it is not ideal, but that is what I have.

Any help will be appreciated. Thanks in advance.

game wu
  • 299
  • 5
  • 17
  • 1
    Make a new component which wraps ProcessorFactory and delegates to its static methods. Inject this component into BankService. Then mock this component. – tgdavies Sep 01 '21 at 23:54
  • @vaibhavsahu That post is very old. I was expecting it should be supporting mock static now. – game wu Sep 01 '21 at 23:59
  • @gamewu https://javadoc.io/static/org.mockito/mockito-core/3.4.6/org/mockito/Mockito.html#static_mocks – vaibhavsahu Sep 02 '21 at 00:00
  • @tgdavies That is the nice workaround, but our code has many many place like that, this workaround will be the last option. – game wu Sep 02 '21 at 00:00
  • @vaibhavsahu That is mockito-inline, and same as what I have in the code sample, same issue, within try block, assertThat passes, but in onMessage, it won't return mocked object. – game wu Sep 02 '21 at 00:03
  • It's not a workaround, it's the right way top do it :-) – tgdavies Sep 02 '21 at 00:22
  • ProcessorFactory.getProcessor(..) is not a static method. Maybe that’s the problem? – johanneslink Sep 02 '21 at 05:11

0 Answers0