Spock如何在方法中模拟自动装配类的函数调用 [英] Spock How to mock Autowired class' function call within a method
问题描述
我有一个我想测试的类,如下所示:
package com.something;
import org.springframework.beans.factory.annotation.Autowired;
public class ClassToTest implements InterfaceToTest {
@Autowired
AnotherService serviceA;
@Override
public List< String> methodToTest(List< String> randomVar){
...
String stringA = serviceA.someFunction(randomVar);
...
}
}
如何模拟调用serviceA.someFunction(randomVar)返回任何我选择的字符串时使用spock测试的结果?
package com .something;
import spock.lang.Shared $ b $ import spock.lang.Specification $ b $ class TestClass extends Specification {
@Shared InterfaceToTest classToTest = new ClassToTest()
静态doWithSpring = {
serviceA(AnotherService)
}
deftests-part-1(){
when:something
。 ..
then:expect this
...
}
}
我不知道该从哪里出发。我的IDE用添加到测试类中的doWithSpring代码显示错误。如何处理这个问题的任何想法?解决方案
它将serviceA字段设置为这样:
import org.springframework.beans.factory.annotation.Autowired;
public class ClassToTest实现了InterfaceToTest {
private AnotherService serviceA;
@Autowired
public ClassToTest(final AnotherService serviceA){
this.serviceA = serviceA;
}
@Override
public List< String> methodToTest(List< String> randomVar){
...
String stringA = serviceA.someFunction(randomVar);
...
}
}
然后在你的spock单元测试你可以在构造函数中提供一个模拟:
pre $ class TestClass extends Specification {
def mockServiceA = Mock(AnotherService )
@Shared InterfaceToTest classToTest = new ClassToTest(mockServiceA)
在每个测试用例中,您可以用通常的spock方式进行模拟:
1 * mockServiceA.someFunction(_)>> '字符串'
I have a class that I want to test in that looks like this: package com.something;
import org.springframework.beans.factory.annotation.Autowired;
public class ClassToTest implements InterfaceToTest{
@Autowired
AnotherService serviceA;
@Override
public List<String> methodToTest(List<String> randomVar){
...
String stringA = serviceA.someFunction(randomVar);
...
}
}
How can I mock the results from the call to serviceA.someFunction(randomVar) to return any String of my choice when testing with spock?
package com.something;
import spock.lang.Shared
import spock.lang.Specification
class TestClass extends Specification{
@Shared InterfaceToTest classToTest = new ClassToTest()
static doWithSpring = {
serviceA(AnotherService)
}
def "tests-part-1"(){
when: "something"
...
then: "expect this"
...
}
}
I dont know where to go from here. My IDE shows errors with the doWithSpring code I added to the testing class. Any ideas on how to deal with this?
A simple solution to enable unit testing would be to alter ClassToTest to have a constructor which sets the serviceA field like this:
import org.springframework.beans.factory.annotation.Autowired;
public class ClassToTest implements InterfaceToTest{
private AnotherService serviceA;
@Autowired
public ClassToTest(final AnotherService serviceA){
this.serviceA = serviceA;
}
@Override
public List<String> methodToTest(List<String> randomVar){
...
String stringA = serviceA.someFunction(randomVar);
...
}
}
Then in your spock unit test you can provide a mock in the constructor:
class TestClass extends Specification{
def mockServiceA = Mock(AnotherService)
@Shared InterfaceToTest classToTest = new ClassToTest(mockServiceA)
And in each test case you can mock in the usual spock way:
1 * mockServiceA.someFunction(_) >> 'A string'
这篇关于Spock如何在方法中模拟自动装配类的函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!