Spring Framework
SpringDI1

StaticApplicationContext
SpringStaticApplicationContextregisterGuice使

import org.springframework.context.support.StaticApplicationContext;
import static org.mockito.Mockito.*;
class HogeControllerTest {
StaticApplicationContext context;
@BeforeEach
void setup() {
context = new StaticApplicationContext();
context.registerBean(HogeController.class);
context.registerBean(HogeUsecase.class, this::mockHogeUsecase);
}
@Test
void test() {
HogeController controller = context.getBean(HogeController.class);
controller.processHoge();
verify(context.getBean(HogeUsecase.class), times(1)).verifyHoge(any());
}
private HogeUsecase mockHogeUsecase() {
HogeUsecase mock = mock(HogeUsecase.class);
// MockObject
return mock;
}
}

IDE使使()