기존에 Mockito와 JUnit Eclosed를 이용해서 작성한 테스트 코드를 Spock을 이용해서 재작성해봤다. 뭐랄까, 속이 다 후련하다. Spock이 좀 더 익숙해지면 여러 상황을 위한 테스트 코드 만드는데 도움이 될 것 같다.
class PaymentErpSyncSpockSpecification extends Specification {
def PaymentErpSync sync = new PaymentErpSync()
def PaymentSyncSourceDao mockSyncSourceDao = Mock()
def PaymentInfoConverter mockPaymentInfoConverter = Mock();
def ExternalErpClient mockExternalErpClient = Mock();
def OrderSyncResultDao mockOrderSyncResultDao = Mock();
def setup() {
sync.setPaymentSyncSourceDao(mockSyncSourceDao)
sync.setExternalErpClient(mockExternalErpClient)
sync.setPaymentInfoConverter(mockPaymentInfoConverter)
sync.setOrderSyncResultDao(mockOrderSyncResultDao)
}
def "PaymentSyncSourceDao 읽기 실패시"() {
when: "동기화 실행"
sync.syncPaymentInfo()
then: "SyncSource 읽기 실패하고, 실패 결과 기록해야 함"
mockSyncSourceDao.findAllByBeforeSync() >> { throw new RuntimeException() };
mockOrderSyncResultDao.insert(_) >> { OrderSyncResult result ->
assert result.result == false
assert result.syncType == null
assert result.failedSource == "PaymentSyncSourceDao"
}
}
def "PaymentSyncSourceDao 읽기 성공시"() {
setup: "PaymentSyncSourceDao 데이터 제공 설정"
def paymentSyncSources =[
PaymentSyncSource.builder().id(1L).saleDate(new Date()).type("p").build(),
PaymentSyncSource.builder().id(2L).saleDate(new Date()).type("p").build()
]
mockSyncSourceDao.findAllByBeforeSync() >> paymentSyncSources
def paymentInfo = []
paymentSyncSources.each { source ->
mockPaymentInfoConverter.convert(source) >> paymentInfo
}
when: "동기화 실행하면,"
sync.syncPaymentInfo()
then: "ERP 전송 실패하고, 실패 결과 기록해야 함"
2 * mockExternalErpClient.send(paymentInfo) >> { throw new SendFailureException() }
2 * mockOrderSyncResultDao.insert({ OrderSyncResult result ->
result.result == false &&
result.syncType == SyncType.payment &&
result.failedSource == "ExternalErpClient"
})
when: "동기화 실행하면,"
sync.syncPaymentInfo()
then: "모두 성공하고, 성공 결과 기록해야 함"
2 * mockExternalErpClient.send(paymentInfo)
2 * mockOrderSyncResultDao.insert({OrderSyncResult result ->
result.result == true &&
result.syncType == SyncType.payment
})
}
}