我如何检查bar(Alpha, Baz)是否使用Mockito bar(Xray, Baz)使用Mockito-否则请勿实际调用MCVE class Foo: public class Foo { public String bar(Xray xray, Baz baz) { return "Xray"; } public String bar(Zulu zulu, Baz baz) { return "Zulu"; } public String bar(Alpha alpha, Baz baz) { if(alpha.get() instanceof Xray) { return bar((Xray)alpha.get(), baz); } else if(alpha.get() instanceof Zulu) { ret
以下是关于 powermock 的编程技术问答
我(仍在)尝试检查bar(Alpha, Baz)是否使用powermockito(为bar(Xray, Baz)是private) - 在下面的MCVE类Foo时,bar(Xray, Baz)是private). (我经历了同一类早些时有一个déjàvu...) public class Foo { private String bar(Xray xray, Baz baz) { return "Xray"; } private String bar(Zulu zulu, Baz baz) { return "Zulu"; } public String bar(Alpha alpha, Baz baz) { if(alpha.get() instanceof Xray) { return bar((Xray)alpha.get(), baz); }
我似乎无法克服这个问题.我正在尝试模拟一个以1个参数 的超载方法 class ClassWithOverloadedMethod { private boolean isValid(ClassA a){ return true; } private boolean isValid(ClassB B){ return false; } } 模拟设置 ClassWithOverloadedMethod uut = PowerMockito.spy(new ClassWithOverloadedMethod()); PowerMockito.doReturn(true).when(uut, "isValid", Matchers.isA(ClassB.class)); 但是PowerMockito不断返回此错误 java.lang.NullPointerException at java.lang.Cla
当我作为OSGI插件测试进行测试时,我将获得org.hamcrest.matchers的NoclassDeffoundError,但是当我以普通的Junit测试为预期时,它可以按预期运行.我正在使用 osgi版本的powermock ,并且在我的启动配置中具有所有必要依赖性. 我做错了什么?似乎由于某种原因,Testrunner似乎没有看到该类. 编辑: 我创建了一个减少的示例项目,并发现该问题仅在我的类声明中使用@PrepareForTest(XXX.class)时出现. java.lang.NoClassDefFoundError: org/hamcrest/Matchers at eu.gemtec.commons.util.assertion.Assert.assertParamNotNull(Assert.java:107) at eu.gemtec.eagle.device.aastra.omaxi.core.system.model.impl.M
我正在尝试在Legacy Java项目中运行测试,我会收到此错误: java.lang.NoClassDefFoundError: Could not initialize class org.powermock.modules.junit4.rule.PowerMockRule 引起问题的代码就是这样: @Rule public PowerMockRule rule = new PowerMockRule(); 和PowerMock版本是1.5.6 org.powermock powermock-module-junit4 1.5.6 test 即使更新到最新版本的PowerMock - 版本2.0
我正在使用EasyMock + PowerMock编写测试,我使用PowerMock的原因是模拟静态方法. 我的测试课看起来像: import static org.powermock.api.easymock.PowerMock.mockStatic; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest(StaticTest.class) public class DummyT
我正在尝试学习各种模拟库的来信和 powermock ((特别是easymock扩展名)是列表中的下一个.我试图嘲笑构造函数,当我尝试复制它们时,提供的示例没有相同的响应.据我所知,它永远不会嘲笑构造函数,只是继续进行正常. 这是测试类: @RunWith(PowerMockRunner.class) @PrepareForTest({Writer.class}) public class FaultInjectionSituationTest { @Test public void testActionFail() throws Exception { FaultInjectionSituation fis = new FaultInjectionSituation(); PowerMock.expectNew(Writer.class, "test") .andThrow(new IOExceptio
我正在尝试测试此方法. @Override public JSON connectResource() throws IOException { //get the location and credentials for the certificates System.setProperty("javax.net.ssl.trustStore", "C:/Program Files/Java/jdk1.7.0_40/jre/lib/security/cacerts"); System.setProperty("javax.net.ssl.trustStorePassword", "changeit"); HttpRequest httpRequest = new HttpGet(url); System.out.println("hello"); httpRequest.addHeader("Accept", "applicatio
我正在尝试模拟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
此代码只是getPackage和类的名称(尚未使用任何模拟技术),但失败了. 有人以前看到了这个问题吗? 代码: import mai.MyScala1 import org.junit.Test import org.junit.runner.RunWith import org.powermock.modules.junit4.PowerMockRunner import org.scalatest.junit.JUnitSuite @RunWith(classOf[PowerMockRunner]) class MyTest extends JUnitSuite { @Test def test1() { classOf[MyScala1].getPackage // this one returns null classOf[MyScala1].getPackage.getName // raise java.lang
所以我正在使用Java 8,并尝试使用PowerMock和Mockito编写一些测试.我得到的消息MethodNotFoundException: No methods matching the name(s) stream were found in the class hierarchy of class java.util.Arrays$ArrayList. 我仔细检查 arraylist文档而且肯定看起来像从收藏中继承stream.这是PowerMockito还是我错过了什么? 有问题的行 PowerMockito.when(thing.call("services", "things")).thenReturn(Arrays.asList("testService")); // Doesn't matter if it's new ArrayList())); 然后有类似的东西 services.stream().filter(
通过查看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
我正面临着使用Maven的PowerMock和Mockito面临奇怪的构建问题.我可以完美地运行测试Wihtin Ecplise(使用Maven).但是,当我尝试使用CLI或在构建服务器上使用Maven进行测试时,我会得到以下例外: 错误测试: 测试机制:java.lang.classnotfoundexception:org.mockito.internal.progres s.threadsafemockingprogress 我的单位测试看起来像这样: import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertThat; import static org.mockito.Matchers.anyString; import static org.mock
我正在尝试测试以下代码: GSARepository productCatalog = (GSARepository) Nucleus.getGlobalNucleus().resolveName("/atg/commerce/catalog/ProductCatalog"); for (RepositoryItem orderItem : orderItems) { String product = (String) orderItem.getPropertyValue(PropertyNameConstants.PRODUCTID); if (!ProductUtils.isSpecial(product, productCatalog)) { isSpecial = false; break; } } nucleus.getglobalnucleus()是静态的. 根据文档,我应该能够使用: PowerM
我正在使用Springboot和Junit,我想使用PowerMock来模拟静态类,在添加了PowerMock之后,单元测试通过Intellij Idea运行良好,但是当我在终端下运行mvn test时,它会抛出 applicationContextExtexception:无法启动Web服务器\ n无法启动嵌入式tomcat 我的基础测试类: @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = WebApplication.class) @PowerMockRunnerDelegate(SpringRunner.class) @RunWith(PowerMockRunner.class) @PowerMockIgnore( {"javax.management.*", "javax.net.*", "javax.crypto.*"}) @Slf4j @Co
PaymentBusinessService类在BusinessService类依赖注入中可以避免.应用程序sc = applionaition.ApplicationValidation(this.deal);假设是 应用程序SC = BusinessService.ApplicationValidation(this.deal); package com.core.business.service.dp.fulfillment; import com.core.business.service.dp.payment.PaymentBusinessService; public class BusinessServiceImpl implements BusinessService { // Actual Impl Class private PaymentBusinessService paymentBusinessService = PluginSystem.INSTA
嗨,我在尝试编写单元测试用例时会得到无效的指针异常 这是类细节: public CreateDraftCampaignResponse createDraftCampaign(CreateDraftCampaignRequest request) throws InvalidInputsException, DependencyException, UnauthorizedException { CreateDraftCampaignResponse draftCampaignResponse = null; try { DraftCampaignDetails createdDraft = draftCampaignI.createDraftCampaign(ConvertionUtil .getDraftCampaignDetailsfromCreateDraftRequest(request))
在此代码中,我嘲笑了validatemsissisdnhandlerirspy.validate(). . @RunWith(PowerMockRunner.class) @PrepareForTest({ValidateMsisdnHandler.class,ValidateHandlerSoapClient.class}) public class Demo { MessageControl messageControl=PowerMockito.mock(MessageControl.class); Validate validate=PowerMockito.mock(Validate.class); ValidateMsisdnHandlerIR validateMsisdnHandlerIRSpy = PowerMockito.spy(new ValidateMsisdnHandlerIR()); ValidateHandlerSoa
我有一个简单的代码行: DraftCampaignDetails createdDraft = draftCampaignI.createDraftCampaign(ConvertionUtil .getDraftCampaignDetailsfromCreateDraftRequest(request)); 我试图像这样模拟它: ConvertionUtil action1 = PowerMockito.mock(ConvertionUtil.class); when(action1.getDraftCampaignDetailsfromCreateDraftRequest(request)).thenReturn(details); when(draftCampaignI.createDraftCampaign(details)).thenReturn(details); 但是我遇到了这个错误: w