使用PowerMockito和x2B进行Spring集成测试;TestNG导致调用TargetException

使用PowerMockito和x2B进行Spring集成测试;TestNG导致调用TargetException,spring,testng,powermockito,Spring,Testng,Powermockito,我试图在与TestNG的集成测试中用PowerMockito模拟一个静态方法,但到目前为止没有什么乐趣 @PrepareForTest({ HttpCommonClientUtils.class }) public class LiveChannelServiceTestNg extends LiveBaseTestNg { @Autowired private LiveChannelShareService liveChannelService; @Resource(name = "liv

我试图在与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



如果没有整个上下文,我将无法帮助您。您至少应该共享
LiveBaseTestNg
。谢谢,这是LiveBaseTestNg.java文件和BaseTestNg.java文件,