I am trying to write tests in spring java.
Classes i have
public class A {
@Autowired
private B b;
public void handle() {
int xyz = b.call();
}
}
public class B {
@Autowired
private C c;
public int call() {
// Do something else here as well.
return c.callC();
}
}
public class C {
@Autowired
private D d;
public int callC() {
//do something with d
return 123;
}
}
I want to actually call the class B but want to mock the response from C. Don't know whether it's possible or not.
my test
@SpringBootTest
@RunWith(SpringRunner.class)
public class TestA {
@Autowired
private A a;
@Mock
private B b
@Mock
private C c
@Test
public void mytest() {
when(c.callC()).thenReturn(1);
A.handle();
verify(c, times(1)).callC();
}
}
But it's still actually calling the class C and not mocking it. Any help will be appreciated.