0

I've been trying for a while to mock my codes. I'm newby on mocking so lots of things ahead of me to catch. I'm using Powermockito with Mockito and Easymock integration.

I'm having difficulties with mocking a method which is inside the method I wanted to test. So here is an example of the situation I faced:

public class trialClass {
 public static int try2(){    
        return 3;    
    }

    public static int try(int a){    
        return try2() + a;    
    }
}

and my test class is:

@RunWith(PowerMockRunner.class)
@PrepareForTest(trial.class)
public class trialTest {

    @Before
    public void setUp() throws Exception {    
        PowerMockito.mockStatic(trial.class);    

        //Here I expect try2() to return 10, even it return 3
        PowerMockito.when(trial.try2()).thenReturn(10);    
    }

    @Test
    public void testtry() throws Exception {
        //After try2() returns 10 recursively inside my try() method,
        //I expect result to be 11
        Assert.assertEquals(11, trial.try(1));    
    }
}

This question of mine actually comes from my session variable. My session holds some value and an X method returns that value. All I need is to mock that X method recursively and this question just simulates this case.

Thanks for your help guys.

4

3 回答 3

1

你需要使用Mockito.CALLS_REAL_METHODS

所以在你的测试设置中:

PowerMockito.mockStatic(trial.class, CALLS_REAL_METHODS);
于 2013-10-01T09:45:15.553 回答
0

请使用 EasyMock 和 PowerMock 作为解决方案找到您的TrialTest.java的更新版本。PowerMock 易于配置,并且不会干扰大多数现有的 jar。你只需要几个 JARS

  • powermock-easymock-XXX-full.jar
  • Easymock-XXjar

您的代码存在一些问题,我已修复:

  • 代码问题:try是一个 Java 关键字,因此不能在方法名称中使用(例如try(int a))。
  • 代码质量:使用 Java 对象而不是 Java 原语(例如,使用整数而不是 int)。
  • 代码质量:trialClass是一个糟糕的 Java 类名(例如,Java 类名使用大写,除非用于教育目的,否则不要使用通用词 Class),也许是 Trial。

这是更新的代码:

试用.java (CUT)

public class Trial {

    public static Integer try2() {
        return 3;
    }

    public static Integer try1(int a) {
        return try2() + a;
    }
}

工作测试类:TrialTest.java

import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Trial.class)
public class TrialTest {

    @Before
    public void setUp() throws Exception {

        /* Setup */
        PowerMock.mockStaticPartial(Trial.class, "try2");

        /* Mocks */
        // Here I expect try2() to return 10, even it return 3
        EasyMock.expect(Trial.try2()).andReturn(10).atLeastOnce();

        PowerMock.replayAll();

    }

    @Test
    public void testtry() throws Exception {
        // After try2() returns 10 recursively inside my try() method,
        // I expect result to be 11
        /* Test */
        Integer result = Trial.try1(1);

        /* Asserts */
        PowerMock.verifyAll();
        Assert.assertEquals(new Integer(11), result);

    }
}
于 2013-11-11T13:29:06.673 回答
0

编辑

我突然想到,您可能不想更改为 EasyMock 而不是 Mockito,在这种情况下请忽略...

部分模拟是您所追求的关键字。你不想嘲笑一切,只是try2()。您应该使用PowerMock.mockStaticPartial(Class, String...)方法。

代替

PowerMockito.mockStatic(trialClass.class);    

利用

PowerMock.mockStaticPartial(trialClass.class, "try2");

然后进行实际的模拟。

另请注意,您在上面的代码中定义了类trialClass,但trial.class在第二个代码中使用......

于 2013-10-01T09:41:01.433 回答