単体テスト:mock外部依存オブジェクトをビジネスクラスに注入
1245 ワード
エンティティークラス:
単体テスト:
public class ConvertController {
@Resource
private ConvertServiceImpl convertservice;
public APIResult convert() throws Exception {
APIResult result = null;
result = convertservice.getConvertResult();
return result;
}
}
public class ConvertServiceImpl implements ConvertService {
public APIResult getConvertResult() {
}
}
単体テスト:
public class ConvertControllerTest {
//mock
private ConvertController controller;
// mock
private ConvertServiceImpl convertservice;
// test
@Before
public void setUp() throws Exception {
controller = new ConvertController();
convertservice = Mockito.mock(ConvertServiceImpl.class); // Mock
Whitebox.setInternalState(controller, "convertservice", convertservice); //
}
@Test
public void testconvert() throws Exception {
//
APIResult api = new APIResult(0, "error");
// convertservice getConvertResult() api
Mockito.when(convertservice.getConvertResult().thenReturn(api);
//
APIResult result = controller.convert();
//
Assert.assertEquals("{\"code\":0,\"errMsg\":\"error\"}", result.toString());
}
}