Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法实例化与类级实例化的junit测试_Java_Unit Testing_Junit_Junit4_Powermock - Fatal编程技术网

Java 方法实例化与类级实例化的junit测试

Java 方法实例化与类级实例化的junit测试,java,unit-testing,junit,junit4,powermock,Java,Unit Testing,Junit,Junit4,Powermock,代码发布在 现在,当我试图为 第一种情况下,我得到的错误 “需要重播B类” 但同样的junit也适用于第二个案例 我的junit是 @RunWith(PowerMockRunner.class) public class TestClass { @Test public void testDoSomeThing() { B b = createMock(B.class) expectNew(b.CallMe()).andReturns(xxx)

代码发布在

现在,当我试图为 第一种情况下,我得到的错误

“需要重播B类”

但同样的junit也适用于第二个案例

我的junit是

@RunWith(PowerMockRunner.class)
public class TestClass {

    @Test
    public void testDoSomeThing() {
        B b = createMock(B.class)
        expectNew(b.CallMe()).andReturns(xxx)
        A a=new A();

        replayAll();
        a.doSomething();
        verifyAll();
    }
}
你忘了加上

@PrepareForTest({A.class, B.class})
此批注必须包含您正在模拟的类以及将使用这些模拟的类。

以下是一个使用with的解决方案:

TestClass.java

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({ A.class, B.class })
public class TestClass {

    @Test
    public void testDoSomeThing() throws Exception {
        /* Setup */
        B bMock = PowerMock.createMock(B.class);

        /* Mocks */
        PowerMock.expectNew(B.class).andReturn(bMock).atLeastOnce();
        bMock.callMe();

        /* Activate */
        PowerMock.replayAll();

        /* Test */
        A cut = new A();
        cut.doSomething();

        /* Asserts */
        PowerMock.verifyAll();
    }
}
public class A {

    B b = new B();

    public void doSomething() {
        b.callMe();
    }
}
public class B {

    public void callMe() {

    }
}
A.java

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({ A.class, B.class })
public class TestClass {

    @Test
    public void testDoSomeThing() throws Exception {
        /* Setup */
        B bMock = PowerMock.createMock(B.class);

        /* Mocks */
        PowerMock.expectNew(B.class).andReturn(bMock).atLeastOnce();
        bMock.callMe();

        /* Activate */
        PowerMock.replayAll();

        /* Test */
        A cut = new A();
        cut.doSomething();

        /* Asserts */
        PowerMock.verifyAll();
    }
}
public class A {

    B b = new B();

    public void doSomething() {
        b.callMe();
    }
}
public class B {

    public void callMe() {

    }
}
B.java

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({ A.class, B.class })
public class TestClass {

    @Test
    public void testDoSomeThing() throws Exception {
        /* Setup */
        B bMock = PowerMock.createMock(B.class);

        /* Mocks */
        PowerMock.expectNew(B.class).andReturn(bMock).atLeastOnce();
        bMock.callMe();

        /* Activate */
        PowerMock.replayAll();

        /* Test */
        A cut = new A();
        cut.doSomething();

        /* Asserts */
        PowerMock.verifyAll();
    }
}
public class A {

    B b = new B();

    public void doSomething() {
        b.callMe();
    }
}
public class B {

    public void callMe() {

    }
}

你能发布你的JUnit测试吗?你在哪里看到这个错误?这就是你得到的字符串吗?@all粘贴了我的junit类在这里发布之前先格式化你的代码,帮你自己一个忙。java.lang.IllegalStateException:必须重播类xxxx才能得到配置的期望值。在org.powermock.api.easymock.internal.invocationcontrol.NewInvocationControlImpl.invoke(NewInvocationControlImpl.java:60)在org.powermock.core.MockGateway.newInstanceCall(MockGateway.java:169)上,这正是我得到的堆栈跟踪。谢谢。