私はSPOCKを使ってテストケースを書くのに問題があります。誰でも私を助けてくれますか?継承クラスの@Autowired変数でテストクラスを書く方法
Iは以下のようなクラス&インターフェース、以下
//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ために利用可能な任意の代替はありますか?いくつかの解決策を提案してください&あなたのコメント。
*
Iこのように変更する 'public class ctrlClass { @Autowired Aimpl aObj; public int getIdValue(){ 戻り値aObj.getId(); } } 私はインターフェイスAの方法を正しく使用できませんか? 注: 私の現在の継承構造では、AとBの両方のインタフェースメソッドを使用することができます。 –