gpt4 book ai didi

java - EasyMock 和 Annotations 以及 2 个相同类型的依赖项

转载 作者:行者123 更新时间:2023-11-30 08:20:43 24 4
gpt4 key购买 nike

最近,我开始使用 EasyMock 3.2 及其非常喜欢的基于注释的测试功能。但是,当我遇到不知道如何处理的异常时,我遇到了这种情况。我对这种情况进行了建模,并寻求有关如何测试我的类(class)的建议

  1. 假设我有一个类 Line,它包含两个参数 a 和 b,并且可以计算 y(x) = a * x + b 的值.

  2. 假设我有一个接口(interface) Param 来表示 a 和 b(无论出于何种原因,这只是一个示例):

所以我的类看起来像这样:

public class Line {
private Param paramA;
private Param paramB;

public int calculate(int x) {
return paramA.intValue() * x + paramB.intValue();
}

}

接口(interface)参数也非常简单:

public interface Param {
int intValue();
}

现在我要为它创建一个测试。我在 Java 7 上使用 JUnit,在 IntelliJ 中使用 EasyMock 3.2。

import org.easymock.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
import static org.easymock.EasyMock.*;


@RunWith(EasyMockRunner.class)
public class LineTest extends EasyMockSupport {

@TestSubject
private Line testSubject = new Line();

@Mock(name = "paramA") //I've tried with and without 'name'
private Param paramA;

@Mock(name = "paramB")
private Param paramB;

@Test
public void test() {
expect(paramA.intValue()).andReturn(2);
expect(paramB.intValue()).andReturn(4);
replayAll();
int actualResult = testSubject.calculate(3);
// I expect to observe actualResult = 3 * 2 + 4 = 10
assertEquals(10, actualResult);
verifyAll();

}
}

到目前为止一切顺利,但是运行测试会产生以下异常:

java.lang.RuntimeException: At least two mocks can be assigned to private 
Param Line.paramA: paramA and paramB
at org.easymock.EasyMockSupport.injectMocksOnClass(EasyMockSupport.java:665)
at org.easymock.EasyMockSupport.injectMocks(EasyMockSupport.java:640)
at org.easymock.EasyMockStatement.evaluate(EasyMockRunner.java:55)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

看起来 easymock 无法通过字段名称注入(inject)模拟,而是尝试通过类型进行模拟,但因歧义而失败。

我的错误是什么?

提前致谢,祝您有愉快的一天

最佳答案

你读过 docs 了吗?那具体解释这部分?您应该使用 fieldName 限定符来消除此类分配的歧义。

The annotation has an optional element, 'type', to refine the mock as a 'nice' mock or a 'strict' mock. Another optional annotation, 'name', allows setting of a name for the mock that will be used in the createMock call, which will appear in expectation failure messages for example. Finally, an optional element, "fieldName", allows specifying the target field name where the mock should be injected. Mocks are injected to any field in any @TestSubject that is of compatible type. If more than one mock can be assigned to the same field then this is considered an error. The fieldName qualifier can be used in this scenario to disambiguate the assignments.

强调我的。

关于java - EasyMock 和 Annotations 以及 2 个相同类型的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25848460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com