![](/img/trans.png)
[英]JUnit5/Mockito - test coverage on a void method that throws and handles an exception
[英]public void method throws Exception if - JUNIT test: Exception should not be thrown
我有一個特定的public void method
, if
滿足條件,它throws
Exception
。 在我的例子中,該方法如下所示:
public void toBeTestedMethod(Testobject testObject) throws CertainException {
if (testObject.getStatus().getAllowsEdit()){
throw ...}
}
getStatus()
是一種返回特定狀態的方法,而getAllowsEdit()
是一種返回boolean
值和nullable = true
的方法。 對於這兩種方法,也存在集合方法。
Edit1:關於此方法失敗時的測試已經運行良好:
public void testToBeTestedMethod_FailureStatus() throws Exception {
try {
TestObject testObject = _testObjectMockDAO.getNewTestObject();
_testObjectMockDAO.setTestObject(testObject);
_testObjectBusinessImpl.toBeTestedMethod(testObject);
fail("Check failed");
} catch (CertainException ex) {
assertEquals(ErrorCode.WRONG_STATUS, ex.getErrorCode());
}
}
我現在想測試方法toBeTestedMethod
。 目標是該方法不會拋出異常但會成功執行。
這意味着我想編寫一個 JUNIT 測試來測試以下內容:
public void testToBeTestedMethod_success throws Exception{
// Enter test code here
}
Edit2(關於class Status
):
public class Status {
...
private String _status;
public String getStatus() {
return _status;
}
}
在我看來,我必須修改 if 語句中的條件才能獲得預期結果,對嗎?
注意:我沒有寫方法和其他代碼。 盡管如此,我的任務是通過 JUNIT 測試代碼。我嘗試了一些代碼,但每次我都收到拋出 Excpetion 的錯誤。
即使你不能解決這個問題,我也很樂意得到一些提示,我應該在哪里尋找問題,為什么我的測試沒有做我想要測試做的事情。
您的問題非常抽象,需要更多數據,我將根據我的理解在此處發布答案。 以下是課程:
public class SampleTestService {
public boolean toBeTestedMethod(TestObject testObject) throws AccessViolationException {
if (testObject.getStatus().getAllowsEdit()) {
throw new AccessViolationException("Edit is allowed for this non confirmed user");
} else {
return true;
}
}
static class TestObject {
private SomeStatus someStatus;
public SomeStatus getStatus() {
return someStatus;
}
}
static class SomeStatus {
private boolean allowsEdit;
public boolean getAllowsEdit() {
return allowsEdit;
}
}
static class AccessViolationException extends RuntimeException {
public AccessViolationException(String message) {
super(message);
}
}
}
由於該方法依賴於另一個 class,並且該類依賴於另一個 class,因此您需要在鏈中模擬它們。 這是我的做法:
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(SpringExtension.class)
class SampleTestServiceTest {
private final SampleTestService.TestObject mockTestObject = mock(SampleTestService.TestObject.class);
private final SampleTestService.SomeStatus mockSomeStatus = mock(SampleTestService.SomeStatus.class);
private final SampleTestService service = new SampleTestService();
@Test
void testThatMethodDoesNotThrowsException() {
when(mockTestObject.getStatus()).thenReturn(mockSomeStatus);
when(mockSomeStatus.getAllowsEdit()).thenReturn(false);
boolean result = service.toBeTestedMethod(mockTestObject);
Assertions.assertTrue(result);
}
@Test
void testThatMethodThrowsException() {
when(mockTestObject.getStatus()).thenReturn(mockSomeStatus);
when(mockSomeStatus.getAllowsEdit()).thenReturn(true);
Assertions.assertThrows(SampleTestService.AccessViolationException.class, () -> {
service.toBeTestedMethod(mockTestObject);
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.