I am trying to get a unit test to work using Mockito 5.14.2. The method I am trying to unit test is a static method (m1). Within the same class Myclass1 there is another static method (m2) which returns a string and is called by m1. This seems like the case for using a spy. However, it does not work as I expect.
class Myclass1 {
public static String m1() {
// do stuff
return m2();
}
public static String m2() {
// do stuff
return "a string";
}
}
tried:
class Myclass1Test {
@Test
void testM1() {
try (MockedStatic
the return of Myclass1.m1() is always null.
also tried
class Myclass1Test {
@Spy
Myclass1 ut;
@Test
void testM1() {
doReturn("a result").when(ut).m2();
String result = ut.m1();
}
}
but this would not even compile.
My question is how to spy on a class that has a static method that calls another static method within the same class? Or what do I need to do to unit test method Myclass1.m1?