我试图在与TestNG的集成测试中使用PowerMockito模拟一个静态方法,但到目前为止还没有快乐。
@PrepareForTest({ HttpCommonClientUtils.class })
public class LiveChannelServiceTestNg extends LiveBaseTestNg {
@Autowired
private LiveChannelShareService liveChannelService;
@Resource(name = "liveSettingService")
private LiveSettingShareService liveSettingService;
@Autowired
private VCloudHelper vcloudHelper;
@ObjectFactory
public IObjectFactory getObjectFactory() {
return new org.powermock.modules.testng.PowerMockObjectFactory();
}
static String result = "{\\\"cid\\\":\\\"0aba025de6604ccb931bd9868bddba9a\\\",\\\"duration\\\":120,\\\"format\\\":0,\\\"gmtCreate\\\":1486378732486,\\\"hlsPullUrl\\\":\\\"http://pullhls03f9fed7.live.126.net/live/0aba025de6604ccb931bd9868bddba9a/playlist.m3u8\\\",\\\"httpPullUrl\\\":\\\"http://flv03f9fed7.live.126.net/live/0aba025de6604ccb931bd9868bddba9a.flv?netease=flv03f9fed7.live.126.net\\\",\\\"id\\\":1,\\\"name\\\":\\\"直播频道1\\\",\\\"needRecord\\\":1,\\\"pushUrl\\\":\\\"rtmp://p03f9fed7.live.126.net/live/0aba025de6604ccb931bd9868bddba9a?wsSecret=e5cf3b5c060271fd23ba56469a803a99&wsTime=1486378731\\\",\\\"referType\\\":0,\\\"rtmpPullUrl\\\":\\\"rtmp://v03f9fed7.live.126.net/live/0aba025de6604ccb931bd9868bddba9a\\\",\\\"status\\\":0,\\\"type\\\":0}";
static String cid = "0aba025de6604ccb931bd9868bddba9a";
@BeforeClass
public void setUp() throws Exception {
/* 公用的环境初始化... */
}
@BeforeMethod
public void testSetUp() throws Exception {
}
@Test
public void test_static() {
String result = "{\"cid\":\"0aba025de6604ccb931bd9868bddba9a\",\"duration\":120,\"format\":0,\"gmtCreate\":1486378732486,\"hlsPullUrl\":\"http://pullhls03f9fed7.live.126.net/live/0aba025de6604ccb931bd9868bddba9a/playlist.m3u8\",\"httpPullUrl\":\"http://flv03f9fed7.live.126.net/live/0aba025de6604ccb931bd9868bddba9a.flv?netease=flv03f9fed7.live.126.net\",\"id\":1,\"name\":\"直播频道1\",\"needRecord\":1,\"pushUrl\":\"rtmp://p03f9fed7.live.126.net/live/0aba025de6604ccb931bd9868bddba9a?wsSecret=e5cf3b5c060271fd23ba56469a803a99&wsTime=1486378731\",\"referType\":0,\"rtmpPullUrl\":\"rtmp://v03f9fed7.live.126.net/live/0aba025de6604ccb931bd9868bddba9a\",\"status\":0,\"type\":0}";
PowerMockito.mockStatic(HttpCommonClientUtils.class);
PowerMockito.when(HttpCommonClientUtils.getHtmlByPostMethod(Matchers.any(HttpClient.class),
Matchers.any(HttpDataProvider.class))).thenReturn(result);
LiveChannelDto dto = liveChannelService.getOrGenerateChannleByBindInfo(0, 1, 101L);
Assert.assertEquals(cid, dto.getCid());
}
当我运行此测试时,会出现InvocationTargetException