Java 如何使用一个JUnit方法检查多个异常?
我的程序中有这段代码,需要用jUnit进行测试Java 如何使用一个JUnit方法检查多个异常?,java,junit,Java,Junit,我的程序中有这段代码,需要用jUnit进行测试 void deleteCustomer(String name) throws UnknownCustomerException, AccountNotEmptyException { if (name == null) { throw new NullPointerException(); } else if (!exists(name)) { throw new UnknownC
void deleteCustomer(String name) throws UnknownCustomerException,
AccountNotEmptyException {
if (name == null) {
throw new NullPointerException();
} else if (!exists(name)) {
throw new UnknownCustomerException();
} else if (getCustomer(name).deletable()) {
customerList.remove(getCustomer(name));
}
}
我想我可以用一个JUnit方法来测试它,比如
@Test
public void createCustomer(){
System.out.println("createCustomerTest");
try {
element.createCustomer(null);
//fail("Expected an IndexOutOfBoundsException to be thrown");
} catch (NullPointerException anIndexOutOfBoundsException) {
assertTrue(anIndexOutOfBoundsException.getMessage().equals("NullPointerException"));
}
}
正如您所看到的,我已经尝试实现NPE,但没有成功。
如何在一个JUnit方法中检查多个异常?我在网上查看了一些How-To,但也失败了。为每个异常编写自己的测试。无论如何,一次只能抛出一个。
例如,简化方法:
void deleteCustomer( String name ) throws UnknownCustomerException
{
if ( name == null )
{
throw new NullPointerException();
}
else if ( !exists( name ) )
{
throw new UnknownCustomerException();
}
}
然后有两个测试,每个测试都会检查是否引发了异常:
@Test( expected = NullPointerException.class )
public void deleteCustomer_shouldThrowNullpointerIfNameIsNull() throws UnknownCustomerException
{
String name = null;
cut.deleteCustomer( name );
}
@Test( expected = UnknownCustomerException.class )
public void deleteCustomer_shouldThrowUnknownCustomerExceptionIfNameIsUnknown() throws UnknownCustomerException
{
String name = "someUnknownName";
cut.deleteCustomer( name );
}
NullpointerException的问题是,如果在方法中的任何位置抛出NPE,则测试为true/successful/green-因此您应该确保,这不会发生,测试才有意义。为每个异常编写自己的测试。无论如何,一次只能抛出一个。
例如,简化方法:
void deleteCustomer( String name ) throws UnknownCustomerException
{
if ( name == null )
{
throw new NullPointerException();
}
else if ( !exists( name ) )
{
throw new UnknownCustomerException();
}
}
然后有两个测试,每个测试都会检查是否引发了异常:
@Test( expected = NullPointerException.class )
public void deleteCustomer_shouldThrowNullpointerIfNameIsNull() throws UnknownCustomerException
{
String name = null;
cut.deleteCustomer( name );
}
@Test( expected = UnknownCustomerException.class )
public void deleteCustomer_shouldThrowUnknownCustomerExceptionIfNameIsUnknown() throws UnknownCustomerException
{
String name = "someUnknownName";
cut.deleteCustomer( name );
}
NullpointerException的问题是,如果在方法中的任何位置抛出NPE,则测试为true/successful/green-因此,您应该确保这不会发生,测试才有意义。您可以为不同的异常在测试方法中添加几个“catch”语句,例如:
try {
element.createCustomer(null);
Assert.fail("Exception was expected!");
} catch (NullPointerException _ignore) {
} catch (UnknownCustomerException _ignore) {
}
或者使用Java 87
但是如果您从JUnit切换到TestNG,那么您的测试将更加干净:
@org.testng.annotations.Test(expectedExceptions = { NullPointerException.class, UnknownCustomerException.class })
public void createCustomer() throws NullPointerException, UnknownCustomerException {
element.createCustomer(null);
}
关于“expectedException”的更多信息在这里:使用示例可以在这里找到:您可以为不同的异常在测试方法中添加几个“catch”语句,例如:
try {
element.createCustomer(null);
Assert.fail("Exception was expected!");
} catch (NullPointerException _ignore) {
} catch (UnknownCustomerException _ignore) {
}
或者使用Java 87
但是如果您从JUnit切换到TestNG,那么您的测试将更加干净:
@org.testng.annotations.Test(expectedExceptions = { NullPointerException.class, UnknownCustomerException.class })
public void createCustomer() throws NullPointerException, UnknownCustomerException {
element.createCustomer(null);
}
这里有关于“expectedException”的更多信息:可以在这里找到使用示例:我建议您仔细查看的JavaDoc,并为不同的验证实现不同的测试,例如
public class CustomerTest {
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void throwsNullPointerExceptionForNullArg() {
exception.expect(NullPointerException.class);
element.createCustomer(null);
}
@Test
public void throwsUnknwonCustomerExceptionForUnkownCustomer() {
exception.expect(UnknownCustomerException.class);
// exception.expectMessage("Some exception message"); uncomment to verify exception message
element.createCustomer("unknownCustomerName");
}
@Test
public void doesNotThrowExceptionForKnownCustomer() {
element.createCustomer("a known customer");
// this test pass since ExpectedException.none() defaults to no exception
}
}
我建议您仔细看看的JavaDoc,并为不同的验证实现不同的测试,例如
public class CustomerTest {
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void throwsNullPointerExceptionForNullArg() {
exception.expect(NullPointerException.class);
element.createCustomer(null);
}
@Test
public void throwsUnknwonCustomerExceptionForUnkownCustomer() {
exception.expect(UnknownCustomerException.class);
// exception.expectMessage("Some exception message"); uncomment to verify exception message
element.createCustomer("unknownCustomerName");
}
@Test
public void doesNotThrowExceptionForKnownCustomer() {
element.createCustomer("a known customer");
// this test pass since ExpectedException.none() defaults to no exception
}
}
我认为在您的情况下,您应该有单独的测试,但是如果使用Java 8,您可以这样实现: 使用可与JUnit一起使用的断言:
import static org.assertj.core.api.Assertions.*;
@Test
public void test() {
Element element = new Element();
assertThatThrownBy(() -> element.createCustomer(null))
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("NullPointerException");
assertThatThrownBy(() -> element.get(1))
.isInstanceOf(IndexOutOfBoundsException.class);
}
它优于@Test(expected=IndexOutOfBoundsException.class)
或.expected
语法,因为它保证测试中的预期行引发异常,并允许您检查有关异常的更多详细信息,例如消息
我认为在您的情况下,您应该有单独的测试,但是如果使用Java 8,您可以这样实现: 使用可与JUnit一起使用的断言:
import static org.assertj.core.api.Assertions.*;
@Test
public void test() {
Element element = new Element();
assertThatThrownBy(() -> element.createCustomer(null))
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("NullPointerException");
assertThatThrownBy(() -> element.get(1))
.isInstanceOf(IndexOutOfBoundsException.class);
}
它优于@Test(expected=IndexOutOfBoundsException.class)
或.expected
语法,因为它保证测试中的预期行引发异常,并允许您检查有关异常的更多详细信息,例如消息
1)对每个例外情况进行单独的测试;2) 如果你在测试一个异常时失败了,那么在测试其他异常之前先让它工作。我也同意。始终尝试用每种测试方法测试一件事情。除非您想测试异常详细信息(如消息),否则也可以使用;不要抓住它们。使用
assertEquals(a,b)
而不是assertTrue(a.equals(b))
。顺便说一句,你的测试方法名称让我很困惑。你在测试什么?客户删除或客户创建?在测试名称中包括测试应检查的内容也很有用,如createCustumer_shouldThrowNullpointerIf…()1)对每个异常进行单独的测试;2) 如果你在测试一个异常时失败了,那么在测试其他异常之前先让它工作。我也同意。始终尝试用每种测试方法测试一件事情。除非您想测试异常详细信息(如消息),否则也可以使用;不要抓住它们。使用assertEquals(a,b)
而不是assertTrue(a.equals(b))
。顺便说一句,你的测试方法名称让我很困惑。你在测试什么?客户删除或客户创建?在测试名称中包括测试应检查的内容也很有用,例如createCustumer_shouldThrowNullpointerIf…()multicatch已包含在multicatch中,multicatch已包含在其中,看起来退出很好,但我无法使用intellij运行此功能。他们在一个页面上写道:IntelliJ Idea没有特殊配置,只需开始键入assertThat,然后调用两次completion(Ctrl空格)。这看起来很好,但我没有在IntelliJ上运行它。他们写的一个页面是:IntelliJ Idea没有特殊配置,只需开始键入assertThat,然后调用两次completion(Ctrl空格)。