我有一个特定的public void method
,其中throws
和Exception
if
一个条件已满足。
就我而言,该方法如下所示:
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的错误。
即使您不能解决此问题,我也很高兴获得一些提示,可以在哪里查找为什么我的测试未达到我想要的测试目的的问题。
答案 0 :(得分:1)
您的问题非常抽象,需要更多数据,我将根据我的理解在此处发布答案。 这是课程:
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);
}
}
}
由于该方法依赖于另一个类,并且该类也依赖于另一个类,因此您需要在链中模拟它们。这是我的操作方式:
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);
});
}
}