Don't use mockStatic
in this case, you will mock the whole static class, and that's why you won't be able to debug it.
Instead use one of these to avoid mocking the whole static class:
spy
PowerMockito.spy(CasSessionUtil.class)
PowerMockito.when(CasSessionUtil.getCarrierId()).thenReturn(1L);
Or stub
PowerMockito.stub(PowerMockito.method(CasSessionUtil.class, "getCarrierId")).toReturn(1L);
And with stub
, if the method have parameters (e.g. String, and boolean), do the below:
PowerMockito.stub(PowerMockito.method(CasSessionUtil.class, "methodName", String.class, Boolean.class)).toReturn(1L);
And this is the finla code, I choose to use stub
:
@RunWith(PowerMockRunner.class) // replaces the PowerMockRule rule
@PrepareForTest({ CasSessionUtil.class })
public class TestClass extends AbstractShiroTest {
@Autowired
SomeService someService;
@Before
public void setUp() {
Map<String, Object> newMap = new HashMap<String, Object>();
newMap.put("userTimeZone", "Asia/Calcutta");
Subject subjectUnderTest = mock(Subject.class);
when(subjectUnderTest.getPrincipal())
.thenReturn(LMPTestConstants.USER_NAME);
Session session = mock(Session.class);
when(session.getAttribute(LMPCoreConstants.USER_DETAILS_MAP))
.thenReturn(newMap);
when(subjectUnderTest.getSession(false))
.thenReturn(session);
setSubject(subjectUnderTest);
// instead, add the getCarrierId() as a method that should be intercepted and return another value (i.e. the value you want)
PowerMockito.stub(PowerMockito.method(CasSessionUtil.class, "getCarrierId"))
.toReturn(1L);
}
@Test
public void myTestMethod() {
someService.doSomething();
}
}