Tag: powermockito

无法使用mockito模拟私有方法

我试图用power mockto模拟一个私有方法,阅读这篇文章后我得到了一些想法,我遵循相同的结构: 例 这是我的class级: public class test(){ private long verifyMarketEligibilityAndGetOfferDeliveryCalendar(long id) { some lins of code for connectiong to db } public long createOffer(long id){ return verifyMarketEligibilityAndGetOfferDeliveryCalendar(id); } } 这是我的模拟测试: test classUnderTest = PowerMockito.spy(new test()); PowerMockito.doReturn(10).when(classUnderTest, “verifyMarketEligibilityAndGetOfferDeliveryCalendar”, 10l); classUnderTest.createOffer(10); 现在我希望在调用createoffer之后,verifyMarketEligibilityAndGetOfferDeliveryCalendar不会调用,而是返回10个返回但由于某种原因,程序开始执行verifyMarketEligibilityAndGetOfferDeliveryCalendar类,从而执行db相关代码。 有人可以帮忙吗?

如何使用Whitebox模拟私有方法(org.powermock.reflect)

我想模拟一个在另一个方法中调用的私有方法。 以下是我写的示例代码。 Java代码: package org.mockprivatemethods; public class AccountDeposit { // Instantiation of AccountDetails using some DI AccountDetails accountDetails; public long deposit(long accountNum, long amountDeposited){ long amount = 0; try{ amount = accountDetails.getAmount(accountNum); updateAccount(accountNum, amountDeposited); amount= amount + amountDeposited; } catch(Exception e){ // log exception } return amount; } private void updateAccount(long accountNum, long amountDeposited) throws […]

最终类中的Powermock静态最终方法

我写的测试用例: public class AClassUnderTest { // This test class has a method call public Long methodUnderTest() { // Uses the FinalUtilityClass which contains static final method FinalUtilityClass.myStaticFinalMethod(); // I want to mock above call so that test case for my “methodUnderTest” passes } } 我有一个最后一堂课。 public final class FinalUtilityClass { /** * Method has 3 […]

PowerMockito .when()。thenReturn(),randomUUID没有返回预期值

我正在尝试测试连接到包含JCR节点的SQL Server数据库的Web服务方法,因为我们正在使用JackRabbit。 该方法如下: public String addDocumentByJson(String fileName, byte[] fileContent, int status, String userName, String jsonProperties) { UUID id = UUID.randomUUID(); // It does a bunch of operations here return jsonResult; } 其中jsonResult是一个与此类似的对象: { “id” : “” “version” : 1 } 现在,当我尝试按照本答案中的步骤和本文中的代码测试它时,我发现了以下代码(正如我所说的基于过去的链接): @PrepareForTest({ UUID.class }) @RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class) @ContextConfiguration(“/TestSpringConfig.xml”) public class TestJackRabbitService { @Autowired @Qualifier(“jackRabbitService”) IJackRabbitService jackRabbitService; […]

如何在对模拟的不同调用中返回不同的值?

我有以下代码从DB获取当前计数器值。 然后它更新DB中的计数器,然后再次检索该值。 int current = DBUtil.getCurrentCount(); DBUtil.updateCount(50);// it updates the current count by adding 50 int latest = DBUtil.getCurrentCount(); 我想以这样的方式模拟静态方法,即第一个调用应返回100,第二个调用应返回150.如何使用PowerMockito实现此目的? 我正在使用TestNG,Mockito和PowerMock。

如何使用Mockitovalidation重载方法的调用次数?

我如何检查bar(Alpha, Baz)使用Mockito调用bar(Xray, Baz) – 在没有实际调用后者的情况下,给定我的MCVE类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) { return bar((Zulu)alpha.get(), baz); } else […]

powermockito:如何在枚举中模拟抽象方法

考虑以下(简化)枚举: MyEnum { ONE public int myMethod() { // Some complex stuff return 1; }, TWO public int myMethod() { // Some complex stuff return 2; }; public abstract int myMethod(); } 这用于以下function: void consumer() { for (MyEnum n : MyEnum.values()) { n.myMethod(); } } 我现在想为consumer编写一个unit testing,在每个枚举实例中模拟对myMethod()的调用。 我尝试过以下方法: @RunWith(PowerMockRunner.class) @PrepareForTest(MyEnum.class) public class MyTestClass { @Test […]

PowerMock + Emma – 私有静态方法和其他方法的代码覆盖率显示为0%

我参考了PowerMock: 使用PowerMockito模拟私有方法并在此处应用相同的逻辑。 另外,我在eclipse / STS中安装了EMMA(开源工具),但是当我运行代码时,我看到零%的代码覆盖率。 为什么? public class MyClient { public void publicApi() { System.out.println(“In publicApi”); int result = 0; try { result = privateApi(“hello”, 1); } catch (Exception e) { //Assert.fail(); } System.out.println(“result : “+result); if (result == 20) { throw new RuntimeException(“boom”); } } private static int privateApi(String whatever, int num) throws Exception […]

如何使用PowerMock和Mockito模拟枚举类的实例?

我试着按照这个非常相似的问题的答案提供的例子,但它对我不起作用。 我收到以下错误消息: java.lang.IllegalArgumentException: Cannot subclass final class class com.myproject.test.support.ExampleEnumerable at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:447) at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217) at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378) at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318) at org.powermock.api.mockito.repackaged.ClassImposterizer.createProxyClass(ClassImposterizer.java:123) at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:57) at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:110) at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:58) at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:143) 我需要一个enum class的简单模拟实例。 我不需要模拟它的任何方法。 这是我想要模拟的类: public enum ExampleEnumerable implements IEnumerable { EXAMPLE_ENUM_1(“Test Enum 1”), EXAMPLE_ENUM_2(“Test Enum 2”); final String alias; ExampleEnumerable(final String alias) { this.alias = alias; […]

使用PowerMockito 1.6validation静态方法调用

我正在为类似于下面给出的示例的方法编写JUnit测试用例: Class SampleA{ public static void methodA(){ boolean isSuccessful = methodB(); if(isSuccessful){ SampleB.methodC(); } } public static boolean methodB(){ //some logic return true; } } Class SampleB{ public static void methodC(){ return; } } 我在我的测试类中编写了以下测试用例: @Test public void testMethodA_1(){ PowerMockito.mockStatic(SampleA.class,SampleB.class); PowerMockito.when(SampleA.methodB()).thenReturn(true); PowerMockito.doNothing().when(SampleB.class,”methodC”); PowerMockito.doCallRealMethod().when(SampleA.class,”methodA”); SampleA.methodA(); } 现在我想validation是否调用类Sample B的静态methodC()。 如何使用PowerMockito 1.6实现? 我尝试了很多东西,但似乎并没有为我做好准备。 任何帮助表示赞赏。