– Start
假设我们有如下类。
package demo07;
public class BusinessService {
private static String serviceName;
static {
serviceName = "test";
}
public String getServiceName() {
return serviceName;
}
}
下面我们看看如何测试。
package demo07;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ BusinessService.class })
@SuppressStaticInitializationFor("demo07.BusinessService")
public class BusinessServiceTest {
@Test
public void test() throws Exception {
// 不调用构造函数
BusinessService businessService = new BusinessService();
// 验证
Assert.assertNull(businessService.getServiceName());
}
}
– 更多参见:PowerMock 精萃
– 声 明:转载请注明出处
– Last Updated on 2019-08-17
– Written by ShangBo on 2019-08-17
– End
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/shangboerds/article/details/99688477
内容来源于网络,如有侵权,请联系作者删除!