【问题标题】:public void method throws Exception if - JUNIT test: Exception should not be thrown公共 void 方法抛出异常如果 - JUNIT 测试:不应抛出异常
【发布时间】:2021-02-08 17:56:39
【问题描述】:

我有某个public void method其中throwsExceptionif一个条件满足。 就我而言,该方法如下所示:

public void toBeTestedMethod(Testobject testObject) throws CertainException {
if (testObject.getStatus().getAllowsEdit()){
throw ...}
}

getStatus() 是返回某个状态的方法,getAllowsEdit() 是返回 boolean 值和 nullable = true 的方法。对于这两种方法,还存在 set-methods。

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 测试代码。 我尝试了一些代码,但每次我收到抛出异常的错误。

即使你不能解决这个问题,我也很乐意得到一些提示,我应该在哪里寻找问题,为什么我的测试没有做我想让测试做的事情。

【问题讨论】:

  • 你能说明什么是 getStatus 以及类定义的样子吗

标签: java testing exception junit throw


【解决方案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);
        });

    }
}

【讨论】:

  • 感谢您的回答!我认为这是朝着正确的方向发展。我将使用引发异常的测试用例编辑我的问题。这个已经可以了。我还尝试回答您提出的关于 Status 类和 getStatus() 方法的问题。
猜你喜欢
  • 2013-02-15
  • 2016-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-09
  • 2020-08-18
  • 2021-10-28
相关资源
最近更新 更多