Easymock 新对象并处理其函数调用(无 PowerMock)
Easymock new object and handle its function call (No PowerMock)
我有一个 class 需要进行单元测试。结构如下:
public class classToBeTested{
public returnType function1(){
//creation of variable V
return new classA(V).map();
}
}
classclassA如下:
public class classA{
//Constructor
public returnType map(){
//Work
}
}
我正在使用 FunSuite、GivenWhenThen 和 EasyMock 在 Scala 中创建单元测试。
我的测试结构如下:
class classToBeTested extends FunSuite with GivenWhenThen with Matchers with EasyMockSugar {
test(""){
Given("")
val c = new classToBeTested()
expecting{
}
When("")
val returnedResponse = c.function1()
Then("")
//checking the returned object
}
}
期望中需要写什么?
我该如何处理上述情况?
注意:不能使用PowerMock
Answer:
Thanks, @Henri. After a lot of searching and the answer provided by @Henri refactoring the code is the best way to handle this situation. Reason below:
单元测试无法模拟由 new
调用(没有 PowerMock)创建的对象。因此,为了测试代码,我们需要根据正在使用的 class(此处 classA
)和要测试的 class(此处 classToBeTested
).因此,在测试classToBeTested
时,我们需要了解classA
的功能和结构,并分别创建测试用例。
现在测试用例依赖于 classA
中的方法结构,这意味着 classToBeTested
和 classA
是紧密耦合的。因此,通过 TDD 方法,我们需要重构代码。
在上面的例子中:
而不是使用
classA object = new classA(V);
最好将对象提供给方法(例如:在 Spring MVC 中自动装配 classA object
)。
Open to suggestions. Also if somebody could give a better explanation please do that.
你不能。您想要模拟的实例化在您想要测试的 class 中。因此,如果没有 powermock,您需要重构才能使其工作。
最低限度是将 class 创建提取到另一个方法中
public class classToBeTested{
public returnType function1(){
//creation of variable V
return getClassA(V).map();
}
protected classA getClassA(VClass v) {
return new classA(v);
}
然后,您可以进行部分模拟。我不知道如何在 scala 中执行此操作,所以下面的代码可能是错误的,但我希望你能理解。
class classToBeTested extends FunSuite with GivenWhenThen with Matchers with EasyMockSugar {
test(""){
Given("")
val c = partialMockBuilder(classToBeTested.class).addMockedMethod("getClassA").build()
val a = mock(classA.class)
expecting{
expect(c.getClassA()).andReturn(a)
expect(a.map()).andReturn(someReturnType)
}
When("")
val returnedResponse = c.function1()
Then("")
//checking the returned object
// whatever you need to check
}
}
我有一个 class 需要进行单元测试。结构如下:
public class classToBeTested{
public returnType function1(){
//creation of variable V
return new classA(V).map();
}
}
classclassA如下:
public class classA{
//Constructor
public returnType map(){
//Work
}
}
我正在使用 FunSuite、GivenWhenThen 和 EasyMock 在 Scala 中创建单元测试。
我的测试结构如下:
class classToBeTested extends FunSuite with GivenWhenThen with Matchers with EasyMockSugar {
test(""){
Given("")
val c = new classToBeTested()
expecting{
}
When("")
val returnedResponse = c.function1()
Then("")
//checking the returned object
}
}
期望中需要写什么?
我该如何处理上述情况?
注意:不能使用PowerMock
Answer: Thanks, @Henri. After a lot of searching and the answer provided by @Henri refactoring the code is the best way to handle this situation. Reason below:
单元测试无法模拟由 new
调用(没有 PowerMock)创建的对象。因此,为了测试代码,我们需要根据正在使用的 class(此处 classA
)和要测试的 class(此处 classToBeTested
).因此,在测试classToBeTested
时,我们需要了解classA
的功能和结构,并分别创建测试用例。
现在测试用例依赖于 classA
中的方法结构,这意味着 classToBeTested
和 classA
是紧密耦合的。因此,通过 TDD 方法,我们需要重构代码。
在上面的例子中:
而不是使用
classA object = new classA(V);
最好将对象提供给方法(例如:在 Spring MVC 中自动装配 classA object
)。
Open to suggestions. Also if somebody could give a better explanation please do that.
你不能。您想要模拟的实例化在您想要测试的 class 中。因此,如果没有 powermock,您需要重构才能使其工作。
最低限度是将 class 创建提取到另一个方法中
public class classToBeTested{
public returnType function1(){
//creation of variable V
return getClassA(V).map();
}
protected classA getClassA(VClass v) {
return new classA(v);
}
然后,您可以进行部分模拟。我不知道如何在 scala 中执行此操作,所以下面的代码可能是错误的,但我希望你能理解。
class classToBeTested extends FunSuite with GivenWhenThen with Matchers with EasyMockSugar {
test(""){
Given("")
val c = partialMockBuilder(classToBeTested.class).addMockedMethod("getClassA").build()
val a = mock(classA.class)
expecting{
expect(c.getClassA()).andReturn(a)
expect(a.map()).andReturn(someReturnType)
}
When("")
val returnedResponse = c.function1()
Then("")
//checking the returned object
// whatever you need to check
}
}