Java mockito UnfinishedStubingException代替所需的异常

Java mockito UnfinishedStubingException代替所需的异常,java,junit,mockito,Java,Junit,Mockito,我试图模拟一个客户端响应不好的服务进行测试。 我仍然在min eclass中得到一个未完成的存根异常,而不是ClientResponseFailure,我看不出哪里做错了。 我也试过: Mockito.when(hrServiceBad.verifyIdentity("412", "aaa", "aaa")).thenThrow(clientResponseFailure); 但是给出了相同的结果 地雷代码: @Autowired private CheckUserServiceImpl c

我试图模拟一个客户端响应不好的服务进行测试。 我仍然在min eclass中得到一个未完成的存根异常,而不是ClientResponseFailure,我看不出哪里做错了。 我也试过:

 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。

您使用的Mockito.doThrow是错误的

您的代码:

Mockito.doThrow(clientResponseFailure).when(hrServiceBad.verifyIdentity("412", "aaa", "aaa"));
但是
when
方法只需要将模拟作为单个参数:

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

您使用Mockito.doThrow时出错

您的代码:

Mockito.doThrow(clientResponseFailure).when(hrServiceBad.verifyIdentity("412", "aaa", "aaa"));
但是
when
方法只需要将模拟作为单个参数:

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

您使用Mockito.doThrow时出错

您的代码:

Mockito.doThrow(clientResponseFailure).when(hrServiceBad.verifyIdentity("412", "aaa", "aaa"));
但是
when
方法只需要将模拟作为单个参数:

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

您使用Mockito.doThrow时出错

您的代码:

Mockito.doThrow(clientResponseFailure).when(hrServiceBad.verifyIdentity("412", "aaa", "aaa"));
但是
when
方法只需要将模拟作为单个参数:

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

存在多个故障。 第一个确实像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);

测试失败。

存在多个故障。 第一个确实像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);

测试失败。

存在多个故障。 第一个确实像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);

测试失败。

存在多个故障。 第一个确实像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);

测试失败。

现在我总是得到EsbOfflineException,所以在HrService中不会抛出异常。现在我总是得到EsbOfflineException,所以在HrService中不会抛出异常。现在我总是得到EsbOfflineException,所以在HrService中不会抛出异常。现在我总是得到EsbOfflineException,所以在HrService中不会抛出异常。