【发布时间】:2016-12-28 14:30:20
【问题描述】:
我在使用 SPOCK 编写测试用例时遇到问题。谁能帮帮我?
我有如下类和接口,
//Helper class
public class ObjClass{
//Defining all property variables & corresponding getters & setters methods.
}
//Interface
public interface B{
//Declaring custom methods for Mongo repository.
public int getId();
}
public interface A extends MongoRepository<ObjClass, Serializable>, B{
//Defining some standard MongoRepository methods here
}
// Implementation Classes
public class Aimpl implements B{
//implementing all B interface methods
}
public class ctrlClass{
@Autowired
A aObj;
public int getIdValue(){
return aObj.getId();
}
}
以下是对应的SPOCK测试用例:
class test extends Specification
{
ctrlClass obj1
A obj2 //interface class object
def setup(){
obj1 = new ctrlClass();
obj2 = new Aimpl(); //Creating object for interface using impl class.
obj1.aObj = obj2
}
def "test"(){
when:
def a = obj2.getIdValue()
then:
//validating some conditions here with 'a' value
}
}
执行上述测试用例时出现以下错误,
无法将对象 Aimpl 转换为 A 类。
上述相同的场景在 Spring @Autowired 上运行良好。但不是在斯波克。
*
SPOCK 中的@Autowired 是否有任何替代方案可用?请向我推荐一些解决方案和您的 cmets。
*
【问题讨论】:
标签: spring groovy spring-boot junit spock