gpt4 book ai didi

java - 使用 EasyMock 在子类中模拟父类(super class)的对象

转载 作者:太空宇宙 更新时间:2023-11-04 12:24:36 27 4
gpt4 key购买 nike

我有一个要测试的类,如下所示:

public class MainClass extends BaseClass {

public static int variableToBeAsserted= 0;

MainClass(ConfigClass config) {
super(config);
}

public void myMethod() {
List list = objectOfClass1inSuperClass.operation(objectOfClass2inSuperClass.method())
while(methodInSuperClass()) {
// doing operations with 'list'
variableToBeAsserted++;
}
}

// ..few other methods which I am not going to test.
}

我已经抑制了 BaseClassConfigClass 的构造函数。现在我的测试类是这样的:

@RunWith(PowerMockRunner.class)
@PrepareForTest(MainClass.class)
public class TestClass {

@Before
public void setUp(){
suppress(constructor(BaseClass.class))
suppress(constructor(ConfigClass.class))
}

@Test
public void testMyMethod(){
MainClass main = new MainClass(new ConfigClass(""));

List list1= new ArrayList();
test1.add("somevalues");

Class1inSuperClass ob1 = PowerMock.createMock(Class1inSuperClass.class);
Class2inSuperClass ob2 = PowerMock.createMock(Class2inSuperClass.class);

EasyMock.expect(ob2.method()).andReturn(getClass());
EasyMock.expect(ob1.operation(getClass())).andReturn(list1);

PowerMock.replayAll();
main.myMethod();

Assert.assertEquals(expectedValue, main.variableToBeAsserted);
}
}

现在我不知道为什么,但我的测试用例失败并出现 NullPointerException

它尝试访问objectofClass1inSuperClass并失败。我以为这会 mock 它。但它不会被 mock 。

编辑:我只编写测试,无法更改BaseClass中的任何内容。不过,我可以选择修改 MainClass

最佳答案

您有两种方法将模拟对象注入(inject)到测试对象中。

通过WhiteBox手动操作

@RunWith(PowerMockRunner.class)
@PrepareForTest(MainClass.class)
public class WhiteBoxApproachTestClass {

@Before
public void setUp() throws Exception {
suppress(constructor(BaseClass.class));
}


@Test
public void testMyMethod() {
MainClass main = new MainClass(createMock(ConfigClass.class));

List<String> list1 = new ArrayList<>();
list1.add("somevalues");

Class1inSuperClass ob1 = createMock(Class1inSuperClass.class);
Class2inSuperClass ob2 = createMock(Class2inSuperClass.class);

expect(ob2.method()).andReturn(getClass());
expect(ob1.operation(getClass())).andReturn(list1);

Whitebox.setInternalState(main, "objectOfClass1inSuperClass", ob1);
Whitebox.setInternalState(main, "objectOfClass2inSuperClass", ob2);

replayAll();

main.myMethod();

assertThat(MainClass.variableToBeAsserted).isEqualTo(5);
}
}

并通过@TestSubject ( http://easymock.org/user-guide.html#mocking-annotations )

@RunWith(PowerMockRunner.class)
@PrepareForTest(MainClass.class)
public class TestSubjectApproachTestClass {

@Mock(fieldName = "objectOfClass1inSuperClass")
private Class1inSuperClass ob1;

@Mock(fieldName = "objectOfClass2inSuperClass")
private Class2inSuperClass ob2;

@TestSubject
private final MainClass main = new MainClass(createMock(ConfigClass.class));

@BeforeClass
public static void before() throws Exception {
suppress(constructor(BaseClass.class));
}

@Test
public void testMyMethod() {

List<String> list1= new ArrayList<>();
list1.add("somevalues");

expect(ob2.method()).andReturn(getClass());
expect(ob1.operation(getClass())).andReturn(list1);

EasyMockSupport.injectMocks(main);

replayAll();

main.myMethod();

assertThat(MainClass.variableToBeAsserted).isEqualTo(5);
}
}

完整代码您可能会找到here .

关于java - 使用 EasyMock 在子类中模拟父类(super class)的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38502419/

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