我正在写 J-Unit测试现在发生了这个问题: 我正在测试使用实用程序类的servlet(类是最终的,所有方法都是静态的).使用的方法返回空白,可以扔 ioexception(httpresponse.getWriter). 现在我必须强迫这个例外... 我尝试并搜索了很多,但是我发现的所有解决方案都没有起作用,因为有no combination of final, static, void, throw. 以前有人做过吗? 编辑: 这是代码片段 servlet: protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { try { String action = request.getParameter("action"); if (action.equals("saveRule"
以下是关于 powermock 的编程技术问答
我的项目中有一些静态的util方法,其中一些方法只是通过或抛出例外.有很多示例有关如何模拟具有返回类型以外的静态方法的静态方法.但是,我如何模拟一种静态方法,该方法将void返回" doNothing()"? 非空格版本使用以下代码行: @PrepareForTest(StaticResource.class) ... PowerMockito.mockStatic(StaticResource.class); ... Mockito.when(StaticResource.getResource("string")).thenReturn("string"); 但是,如果应用于返回void的StaticResources,则编译会抱怨when(T)不适用于void ... 有什么想法? 解决方法可能只是让所有静态方法返回一些Boolean以获得成功,但我不喜欢解决方法. 解决方案 自Mockito 3.4.0以来,引入了一个实验A
使用Spring Boot启动测试测试我的应用程序,但我正在使用第三方库.让我们假设我们有一个triquest,它有一些构造函数,我想模拟该构造函数以返回结果. @SpringBootTest @RunWith(SpringRunner.class) @PrepareForEverythingForTest public class TestClass { @MockBean TRequest trequest ; @Before public void setUp() throws Exception { PowerMockito.whenNew(TRequest.class).withAnyArguments().thenReturn(trequest); } } 现在,当我尝试使用新的新构造函数创建构造函数时,它没有返回正确的固执结果. TRequest trequest1 = new TRequest("apiKey","secretKey")
我正在尝试模拟PortalUtil.getPortal() so 的方法 PowerMock.mockStatic(PortalUtil.class); Portal mockPortal = Mockito.mock(Portal.class); Mockito.when(PortalUtil.getPortal()).thenReturn(mockPortal); 我得到以下错误 org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.getArticles()).thenReturn(articles); Also, this error might show up because: 1. you stu
通过查看Lauri在 mockito of SecurityManager的Mockito of SecurityManager抛出一个例外我通过嘲笑安全经理写了一个单位测试.以下是测试案例 @RunWith(PowerMockRunner.class) @PrepareForTest(System.class) public class TestClass { @Test public void testcheckSecurity() { //mocking the System class PowerMockito.mockStatic(System.class); SecurityManager secMan = PowerMockito.mock(SecurityManager.class); PowerMockito.when(System.getSecurityManager()).then
public class A{ private final B b; public void meth() { //Some code Integer a = b.some_method(a,fun(b)); //Some code } private fun(int b) { return b; } } when(b.some_method(anyInt(),anyInt())).thenReturn(100) 在编写单元测试类别的单位测试时,如何模拟外部依赖.当我以上述方式模拟依赖性时," A"的值不会按预期分配给100. 解决方案 实际上Jakub的答案是正确的.也许您需要一个示例来了解如何做.检查主要方法和我的示例的创建器. public class A { private final B b; public A(B b) { this.b = b;
嗨,我在尝试编写单元测试用例时会得到无效的指针异常 这是类细节: public CreateDraftCampaignResponse createDraftCampaign(CreateDraftCampaignRequest request) throws InvalidInputsException, DependencyException, UnauthorizedException { CreateDraftCampaignResponse draftCampaignResponse = null; try { DraftCampaignDetails createdDraft = draftCampaignI.createDraftCampaign(ConvertionUtil .getDraftCampaignDetailsfromCreateDraftRequest(request))
@PrepareForTest({...,..., XXX.class}) ... @Test public void testMethodToBeTested(){ XXX mockInstance = PowerMock.createMock(XXX.class); ... PowerMock.expectNew(XXX.class, p1, p2, p3, p4, p5).andReturn(mockInstance); mockInstance.methodWithNoReturnVal(); expect(mockInstance.getSearchVal()).andReturn(1); PowerMock.replay(mockInstance); Whitebox.invokeMethod(objInstance, "methodToBeTested"); PowerMock.verify(mockIns
我测试包含: 的自定义视图类 Android UI元素 一些逻辑 静态方法呼叫 Dagger2依赖项 所以我使用下一个工具进行测试 UI元素嘲笑的robolectric 逻辑测试的单元测试 静态方法的PowerMock模拟 robolectric + powermock集成问题是已知的,解决方案是 .com/robolectric/robolectric/wiki/lust-powermock 但是使用此解决方案dagger2依赖性失败. 注意代码. 我的自定义视图: public class ProgressTextView extends TextView { private String defaultText; private int fileSize; private String fileSizeString; private FileDownloaderI fileDownloader;
我正在使用带有easymock的PowerMock,想知道我怎么会用私人构造函数嘲笑单身人士? 假设我有以下课程: public class Singleton { private static Singleton singleton = new Singleton(); private Singleton() { } public static Singleton getInstance() { return singleton; } public int crazyServerStuff() { ... } } 和一个使用此操作的类: public class Thing { public Thing() {} public int doStuff(Singleton s) { return s.crazyServerStuff() + 42; } } 我如
以下是我的junit测试案例: import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.Date; import java.util.HashMap; import static org.easymock.EasyMock.expect; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.easymock.annotation.Mock; import org.powermock.core.classloader
我正在使用PowerMockrunner来运行我的单元测试.我想从我的资产文件夹加载一些罐头网络响应JSON文件. 我正在使用此方法尝试获取文件. private static File getFileFromPath(Object obj, String fileName) { ClassLoader classLoader = obj.getClass().getClassLoader(); URL resource = classLoader.getResource(fileName); return new File(resource.getPath()); } 我从我的班级中调用这样的方法,该方法在顶部具有这些注释. @RunWith(PowerMockRunner.class) @PrepareForTest(Network.class) File file = getFileFromPath(t
我正在尝试对此代码进行单元测试: Bundle cidParam(String accountId) { Bundle params = new Bundle(1); params.putString(Params.CID, accountId); return params; } 这是单元测试: private void mockBundle(String cid) throws Exception { Bundle mBundle = PowerMockito.mock(Bundle.class); PowerMockito.doNothing().when((BaseBundle)mBundle).putString(AnalyticsController.Params.CID, cid); } 但是,它总是返回: java.lang.RuntimeException: Method putString in an
我被困在一个非常奇怪的情况下. 我有一些需要测试的特定代码. 在这里是: public class A { /* * The real method of real class is so big that I just don't want to test it. * That's why I use throwing an exception. */ protected void method(Integer result) { throw new RuntimeException("Oops!"); } protected T generifiedMethod(String s, T type) { throw new RuntimeException("Oops!"); } protected void mainMethod(Integer value
我有一个使用Securerandom实例的类,并抓住下一个随机数. 假设一个例子是: public class ExampleClass() { public void method() { Random sr = new SecureRandom(); System.out.printf("%d %n", sr.nextInt(1)); System.out.printf("%d %n", sr.nextInt(1)); } } 测试代码 @RunWith(PowerMockRunner.class) public class ExampleClassTest { ... @Test @PrepareOnlyThisForTest(SecureRandom.class) public void mockedTest() throws Exception { Random spy = PowerMockit
现在,我想使用Junit 5 + Mockito 4.x版本 + Mockito-Inline 4.x版本,而不是Junit 4 + PowerMock 2.0.9 因为Junit 5不支持PowerMock也可以模拟静态,所以看起来不需要PowerMock. 但是,当我使用Mockito Mock静态时,我想使用相同的效果,例如PowerMock.whennew(xxx.class).WithArgument(1,2,3,4). 这是我的代码的一部分,可以工作. @Test void get_report_page() { ReportPageRequest reportPageRequest = prepare_request(); prepare_reportPage(context, 9999L, pageable); when(reportConverter.toReportSpecifica
我有一个示例测试类,我想模拟一个静态类.我的build.gradle就像 testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.2.0' testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.2.0' testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.2.0' testRuntime("org.junit.platform:junit-platform-launcher:1.1.1") testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.0-bet
是否可以使用Mockito注释模拟抽象类并向其注入模拟的类.我现在有以下情况: @Mock private MockClassA mockClassA; @Mock private MockClassB mockClassB; @Mock(answer = Answers.CALLS_REAL_METHODS) private AbstractClassUnderTest abstractClassUnderTest; @Before public void init() { MockitoAnnotations.initMocks(this); Whitebox.setInternalState(abstractClassUnderTest, mockClassA); Whitebox.setInternalState(abstractClassUnderTest, mockClassB); } 我想在AbstractClassundertest上
为什么我的单位测试在独立运行时会通过,但在进行多个测试时失败? 当我执行单个单元测试时,我的测试将成功模拟并返回预期的结果.但是,当我运行所有单位测试时,我先前通过的测试将失败. 一次测试运行 应该dondothisagain() - 通行证 多个测试运行 syreddothis() - 通行证 应该doothisagain() - 失败 应该dothisagainagain() - 失败 我的测试: @PrepareForTest({OtherMethods.class}) @PowerMockIgnore("javax.management.*") @RunWith(PowerMockRunner.class) public class DbTest { @Test public void shouldDoThis() throws Exception() { Dal dalMock = mock(
我正在尝试将PowerMock-Mockito和Mockito添加到Android测试项目中.我使用Android命令行工具创建了Android测试项目,以创建build.xml和项目结构.我的libs/目录中有以下罐子: dexmaker-1.0.jar dexmaker-mockito-1.0.jar Mockito-All-1.9.5.jar PowerMock-Mockito-1.5-full.jar 当我尝试通过Ant调试构建项目时,我会收到以下错误: [apkbuilder] Creating ProjectTests-debug-unaligned.apk and signing it with a debug key... [apkbuilder] Found duplicate file for APK: mockito-extensions/org.mockito.plugins.MockMaker [apkbuilder] Origin 1: