2016-08-05 3 views
1
私が代わりにJUnitののスポック枠組みを記述しようとしています

モッキングはスポックにフレームワーク

テストクラスを動作していない:私はきれいなコマンドをインストールMavenを使用して上記のコードを実行すると

class StudentServiceSpec extends Specification{ 

@Shared def studentDao 
@Shared def studentService 

def setupSpec(){ 
    studentDao = Mock(StudentDao) 
    studentService = new StudentService(studentDao) 
} 

def "Get Student Details Based on StudentId"(){ 

    setup: 
    1*studentDao.getStudent(67) >> new Student() 

    when: 
    Response response = studentService.getStudent("67") 
    println "** Response "+response 
    println "** Response "+response.getEntity() 

    then: 
    response != null 
    } 
} 

、私は取得しています次のエラー。

エラー:

1*studentDao.getStudent(67) >>> new Student() (0 invocations) 

私はresponse.getEntity()を取得しています0*studentDao.getStudent(67) >>> new Student() を使用した場合

は、なぜこれが動作していないことを多くの理由があります null

答えて

2

私は、我々が@Sharedを使用している場合は、そのはclassをからかったがからかっていないこの2行

StudentDao studentDao = Mock() 
StudentService studentService = new StudentService(studentDao) 

で次のコード

@Shared def studentDao 
@Shared def studentService 

def setupSpec(){ 
studentDao = Mock(StudentDao) 
studentService = new StudentService(studentDao) 
} 

を置き換え...

を私のミスを発見しましたmethod call

+0

自分の答え – kazanaki

+0

うわー、あなたは私の夜を保存しました:) thx – radio

0

です。

理由の1つは、実際のコードとテストで使用しているパラメータのデータ型に矛盾がある可能性があることです。たとえば、以下のように答えてください

studentDao.getStudent(67) 

あなたのDaoメソッドgetStudentが長いデータ型またはintデータ型を受け入れるかどうかを確認してください。あなたの実際のコードではgetStudentメソッドは長いデータ型しか受け付けませんが、67はあなたのspockテストでintとして扱われるかもしれません。したがって、studentDao.getStudent(67)の呼び出しを偽って新しいStudent()を返すことに失敗しました。

他は、IDがので、DAO方法getStudent

の実際の呼び出しの前に変更されたかもしれません。

  1. その長い場合は、あなたのDAOメソッド
の呼び出しの前にIDを変更し、他のコードがある場合は、あなたのテスト
  • チェックで67Lを試してみてください、あなたのstudentDao.getStudent(_)パラメータのデータ型をチェックヌル

    0*studentDao.getStudent(67) >>> new Student() I am Getting response.getEntity() is null 
    

    とその結果については

    学生オブジェクトを返すようにDAOメソッドのないモックがないので、ヌルが期待されています。

  • 関連する問題