PowerMock 抑制初始化字段

x33g5p2x  于2021-12-28 转载在 其他  
字(0.8k)|赞(0)|评价(0)|浏览(294)

– Start
假设我们有如下类。

package demo08;

public class BusinessService {
	private String serviceName = "test";
	
	public String getServiceName() {
		return serviceName;
	} 
	
}

下面我们看看如何抑制初始化字段。

package demo08;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ BusinessService.class })
public class BusinessServiceTest {

	@Test
	public void test() throws Exception {
		// 抑制初始化字段
		PowerMockito.suppress(PowerMockito.field(BusinessService.class, "serviceName"));
		BusinessService businessService = new BusinessService();

		// 验证
		Assert.assertNull(businessService.getServiceName());
	}

}

– 更多参见:PowerMock 精萃
– 声 明:转载请注明出处
– Last Updated on 2019-08-17
– Written by ShangBo on 2019-08-17
– End

相关文章