Java/Mockito
[Mockito] static getInstance() 메서드 mocking하여 테스트 하기
2021. 10. 8. 01:30반응형
테스트할 대상 클래스
Class Clock {
private static Clock sMe;
private Clock() {}
public static getInstance() {
if(sMe == null) sMe = new Clock();
return sMe;
}
}
테스트 코드
package com.example.mockedstatic;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.MockedStatic;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class ClockTest {
private static MockedStatic<Clock> mStaticClock;
private static Clock mMockClock;
@BeforeClass
public static void beforeClass() {
mStaticClock = mockStatic(Clock.class);
mMockClock = mock(Clock.class);
}
@AfterClass
public static void afterClass() {
mStaticClock.close();
}
@Test
public void execute_add() {
when(Clock.getInstance()).thenReturn(mMockClock);
// do something
}
}
반응형
'Java > Mockito' 카테고리의 다른 글
[Mockito] Previous MockitoSession was not concluded with 'finishMocking()'. 에러 (0) | 2021.10.25 |
---|---|
How to method test (0) | 2021.10.08 |
[Mockito] void 메서드 mocking 하기. (How to mock void method) (0) | 2021.10.08 |
[Mockito] static method 테스트 (0) | 2021.06.09 |