Skip to content
Advertisement

how to mock static class function with inner method in mockito?

I have a class name HibernateSessionManager which have static method

JavaScript

I trying to mock

JavaScript

i am using following function in @before

JavaScript

My test case is following

JavaScript

it passed but coverage is not touching following code

JavaScript

here is my withSession code

JavaScript

openSession

JavaScript

Advertisement

Answer

Looking at the code and assuming it compiles, I believe the problem is that you have two withSession(...) methods and in the code posted you are trying to mock the wrong one. Here are their signatures:

JavaScript

It was easy to guess as the getMBCSessionByGuid method contains the snippet below with the Function<Session, Mbc_session> being passed as an argument to withSession(...) instead of Consumer<Session>:

JavaScript

As an easy fix, you can just add the following to the test:

JavaScript

and remove the existing mock configuration with a Consumer:

JavaScript

P.S. Just in case, I can easily reproduce the issue on my machine.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement