简体   繁体   English

junit中异常编写单元测试用例的格式

[英]Format of writing unit test cases for exceptions in junit

I am writing test cases for a Java class. It contains an exception case also where the exception is thrown when response from server is not as expected.我正在为 Java class 编写测试用例。它包含一个异常情况,当服务器的响应不符合预期时也会抛出异常。

I am bit confused about the name of that test method.我对该测试方法的名称有点困惑。

The method is Book updateBook(Book book);方法是Book updateBook(Book book);

It throws some exception when right response is not received.当没有收到正确的响应时,它会抛出一些异常。

For that, I have written a unit test case.为此,我编写了一个单元测试用例。 But I am not sure what format should I follow for correct naming of that test method.但是我不确定我应该遵循什么格式来正确命名该测试方法。

Should I name it我应该给它起名字吗

  1. testThrowExceptionWhenResponseIsNull();

OR或者

  1. shouldThrowExceptionWhenBookManagementIsNull();

I would prefer我会比较喜欢

updateBookThrowsExceptionWhenBookManagementIsNull

It names which method is under test and the asserted result for which condition.它命名正在测试的方法以及针对哪种条件断言的结果。

But choose a name which is fitting for you und your colleagues.但是选择一个适合您和您的同事的名字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM