2013-11-28 85 views
0

我試圖嘲笑服務與客戶端測試的壞客戶端。 我仍然在min eclass中得到一個UnfinishedStubbingException而不是ClientResponseFailure,我看不到我做錯了什麼。 我也試過:mockito UnfinishedStubbingException而不是想要異常

Mockito.when(hrServiceBad.verifyIdentity("412", "aaa", "aaa")).thenThrow(clientResponseFailure); 

但給出了相同的結果。

礦代碼:

@Autowired 
private CheckUserServiceImpl checkUserService; 
private HrService hrServiceBad; 
private ClientResponseFailure clientResponseFailure; 
private ClientResponseImpl response = new ClientResponseImpl(); 

@Before 
public void init() { 
    hrServiceBad = Mockito.mock(HrService.class); 
    checkUserService.setHrService(hrServiceBad); 
    clientResponseFailure = new ClientResponseFailure(response); 
} 

@Test(expected = EsbVerificationError.class) 
public void testUserValidInEsbWith412Fault() throws EsbOffLineException, EsbVerificationError { 
    // precondition failed 
    response.setStatus(412); 
    Mockito.doThrow(clientResponseFailure).when(hrServiceBad.verifyIdentity("412", "aaa", "aaa")); 
    checkUserService.verifyUserInEsb("412", "aaa", "aaa"); 
} 

@Test(expected = EsbOffLineException.class) 
public void testUserValidInEsbWith503Fault() throws EsbOffLineException, EsbVerificationError { 
    // service unavailable 
    response.setStatus(503); 
    Mockito.when(hrServiceBad.verifyIdentity("503", "aaa", "aaa")) 
      .thenThrow(clientResponseFailure); 

    checkUserService.verifyUserInEsb("503", "aaa", "aaa"); 
} 

@Test(expected = EsbOffLineException.class) 
public void testUserValidInEsbWith404Fault() throws EsbOffLineException, EsbVerificationError { 
    // page not found 
    response.setStatus(404); 
    Mockito.when(hrServiceBad.verifyIdentity("404", "aaa", "aaa")) 
      .thenThrow(clientResponseFailure); 

    checkUserService.verifyUserInEsb("404", "aaa", "aaa"); 
} 

@Test(expected = EsbOffLineException.class) 
public void testUserValidInEsbWith403Fault() throws EsbOffLineException, EsbVerificationError { 
    // page forbidden 
    response.setStatus(403); 
    Mockito.when(hrServiceBad.verifyIdentity("403", "aaa", "aaa")) 
      .thenThrow(clientResponseFailure); 
    checkUserService.verifyUserInEsb("403", "aaa", "aaa"); 
} 

@Test(expected = EsbOffLineException.class) 
public void testUserValidInEsbWith522Fault() throws EsbOffLineException, EsbVerificationError { 
    // connection timeout 
    response.setStatus(522); 
    Mockito.when(hrServiceBad.verifyIdentity("522", "aaa", "aaa")) 
      .thenThrow(clientResponseFailure); 
    checkUserService.verifyUserInEsb("522", "aaa", "aaa"); 
} 

}

的checkUserServiceImpl:預先

@Override 
public void verifyUserInEsb(final String nationalNumber, final String serviceNumber, 
     final String bafuser) throws EsbOffLineException, EsbVerificationError { 
    String cleanedNationalNumber = BulletinUserManager.keepDigitsOnly(nationalNumber); 
    try { 
     Identity identity = this.hrService.verifyIdentity(cleanedNationalNumber, serviceNumber, bafuser); 
     if (identity != null) { 
      //more code here but not relevant. 
      return; 
     } 
    } catch (ClientResponseFailure e) { 
     logger.info(e.getResponse().getStatus()); 
     if (PRECONDITION_FAILED == e.getResponse().getStatus()) { 
      throw new EsbVerificationError("Hr check failed"); 
     } 
    }   
    throw new EsbOffLineException(); 
} 

THX。

回答

0

其中有多個故障。 第一個確實像Stefan Birkner說的。

二:

@Autowired 
private CheckUserServiceImpl checkUserService; 
private HrService hrServiceBad; 
private ClientResponseFailure clientResponseFailure; 
private ClientResponseImpl response = new ClientResponseImpl(); 

@Before 
public void init() { 
    hrServiceBad = Mockito.mock(HrService.class); 
    checkUserService.setHrService(hrServiceBad); 
} 

@Test(expected = EsbVerificationError.class) 
public void testUserValidInEsbWith412Fault() throws EsbOffLineException, EsbVerificationError { 
    // precondition failed 
    response.setStatus(412); 
    clientResponseFailure = new ClientResponseFailure(response); // add this. 
    Mockito.doThrow(clientResponseFailure).when(hrServiceBad).verifyIdentity("412", "aaa", "aaa"); 
    checkUserService.setHrService(hrServiceBad); 
    checkUserService.verifyUserInEsb("412", "aaa", "aaa"); 
} 

什麼問題嗎?經過一番搜索,我看到response.getStatus給出了代碼0. 在每次測試中直接使用init,並在響應添加到ClientResponseFailure之前先將響應中的錯誤代碼設置爲固定。

當你這樣做:

clientResponseFailure = new ClientResponseFailure(response); 
response.setStatus(412); 

測試失敗。

3

您正在使用Mockito.doThrow錯誤。

您的代碼:

Mockito.doThrow(clientResponseFailure).when(hrServiceBad.verifyIdentity("412", "aaa", "aaa")); 

when方法只需要你的模擬作爲一個參數:

Mockito.doThrow(clientResponseFailure).when(hrServiceBad).verifyIdentity("412", "aaa", "aaa"); 
+0

現在我總是得到EsbOfflineException,所以在HrService中沒有拋出異常。 – chillworld

+1

然後修復你的代碼;) – Brice

相關問題