0
首先是 - 這裏是我的代碼的一部分:爪哇 - 在if語句調用布爾超時功能,因此不會發送電子郵件
public void run() {
try {
_conn = new SmtpConnection(this, _socket);
_state = new SmtpState(_workspace);
_quitting = false;
sendGreetings();
while (!_quitting)
handleCommand();
} catch (SocketTimeoutException ste) {
_conn.send("421 Service shutting down and closing transmission channel");
} catch (Exception e) {
// Closing socket on blocked read
if (!_quitting) {
log.error("Unexpected error handling connection, quitting=", e);
throw new IllegalStateException(e);
}
} finally {
if (null != _state) {
_state.clearMessage();
}
}
}
,我有一個同樣功能的另一個類,被稱爲timeoutonemail
public boolean timeoutOnNextEmail()
{
boolean timeout = true;
return timeout;
}
我有一個單元測試,應該儘量發送E-mail,但是當我打電話timeoutfuntion,測試應該通過 - 因此它不應該發送一封電子郵件
/* @Test(expected = MailException.class)
public void sendEmailFail() throws MailException {
greenMail.timeoutOnNextEmail();
try {
app.sendMail("to", "from", "subject", "body");
} catch (final MailException e) {
throw new MailException(e.toString());
} catch (InterruptedException e) {
e.printStackTrace();
}
assertEquals(0, greenMail.getReceivedMessagesForDomain("to").length);
}*/
我怎樣才能知道我跑funtion,如果我打電話在我的單元測試timeoutfuntion,所以它應該停止這樣做,讓我的線程休眠..我想是這樣
*/* if(timeout && !_quitting){
greenMail.timeoutOnNextEmail();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else
{
handleCommand();
}*/*
和幾個不同選項,但我只是不知道如何正確調用它..
*以便它應該停止這樣做,讓我的線程睡覺*你想停止什麼(什麼是'那')? – nullpointer
你的產品代碼應該從不**知道你在單元測試中做的事情。 –
即時通訊開發的框架,所以它必須有該選項..這就是爲什麼我這樣做.. - 這是我的smtp服務器,它應該超時 - 以便下一封電子郵件不能發送 – minzbuddy