java - How to mock external dependencies for final objects? -
public class a{ private final b b; public void meth() { //some code integer = b.some_method(a,fun(b)); //some code } private fun(int b) { return b; } } when(b.some_method(anyint(),anyint())).thenreturn(100)
how mock externally dependency when writing unit tests class a. when mock dependency in above way, value of "a" not getting assigned 100 expected.
actually answer of jakub correct. maybe need example understand how it. check main method , contructor of example.
public class { private final b b; public a(b b) { this.b = b; } public void meth() { //some code integer = b.some_method(5,fun(5)); //some code system.out.println(a); } private int fun(int b) { return b; } public static void main(string[] args) { b b = mockito.mock(b.class); when(b.some_method(anyint(), anyint())).thenreturn(100); new a(b).meth(); } }
with constructor have set b mock (see last third line in main method). when run main method see output of system.out , 100.
Comments
Post a Comment