JAVA:Catching单元测试和参数解析异常
JAVA:Catching exception when unit testing and parameter parsing
@Test
public void testToString(){
System.out.println("toString");
Address add = new Address("Blackthorn","Kings Lynn","PE30");
BusinessOrganisationDetails instance = new BusinessOrganisationDetails("PEA-1234",
"Smith",add,10,"EA",12);
String expResult = "";
String result = instance.toString();
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
这是我的BusinessOrganisationDetailsclass的测试方法,我在想测试的时候是否需要创建那个class的一个实例并在测试时放入特定的变量。它需要以下字段。
// String customerID, String companyName,
// Address address,int companyDiscount,
// String regionalCode,double totalPrice
如您所见,它需要一个地址类型的字段。那么是否有必要像我在上面的代码中所做的那样在这里创建一个地址 class 的实例。我收到一个错误,因为在创建 BusinessOrganisationDetails class 的实例时它说我需要为 IllegalCustomerIDException class 做一个 try catch。鉴于我的情况,我不确定最好的方法
你能更好地解释一下这个场景吗?我不明白您要测试什么以及您到底担心什么。无论如何,根据我的理解,我会说测试中的 try catch 块不是问题,您还可以使用它来测试是否正确抛出异常,例如,如果将错误的客户 ID 作为参数传递给构造函数.或者,如果您只是假设在您的测试中不应抛出异常,您可以在测试方法签名中插入 throws IllegalCustomerIDException
。
try/catch测试没有问题。有两种方法可以测试您的代码是否无异常完成:
@Test
public void testToString() throws IllegalCustomerIDException {
System.out.println("toString");
Address add = new Address("Blackthorn","Kings Lynn","PE30");
BusinessOrganisationDetails instance = new BusinessOrganisationDetails("PEA-1234",
"Smith",add,10,"EA",12);
String expResult = "";
String result = instance.toString();
assertEquals(expResult, result);
}
@Test
public void testToString() {
System.out.println("toString");
try {
Address add = new Address("Blackthorn","Kings Lynn","PE30");
BusinessOrganisationDetails instance = new BusinessOrganisationDetails("PEA-1234",
"Smith",add,10,"EA",12);
String expResult = "";
String result = instance.toString();
assertEquals(expResult, result);
} catch(BusinessOrganisationDetails e) {
fail(e.getMessage);
}
}
如果不想测试是否抛出异常,可以使用 Rule
和 ExcpectedException
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void testToString() throws IllegalCustomerIDException {
System.out.println("toString");
exception.expect(IllegalCustomerIDException.class);
exception.expectMessage("unkwon customer id 4711");
Address add = new Address("Blackthorn","Kings Lynn","PE30");
BusinessOrganisationDetails instance = new BusinessOrganisationDetails("PEA-1234",
"Smith",add,10,"EA",12);
}
@Test
public void testToString(){
System.out.println("toString");
Address add = new Address("Blackthorn","Kings Lynn","PE30");
BusinessOrganisationDetails instance = new BusinessOrganisationDetails("PEA-1234",
"Smith",add,10,"EA",12);
String expResult = "";
String result = instance.toString();
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
这是我的BusinessOrganisationDetailsclass的测试方法,我在想测试的时候是否需要创建那个class的一个实例并在测试时放入特定的变量。它需要以下字段。
// String customerID, String companyName,
// Address address,int companyDiscount,
// String regionalCode,double totalPrice
如您所见,它需要一个地址类型的字段。那么是否有必要像我在上面的代码中所做的那样在这里创建一个地址 class 的实例。我收到一个错误,因为在创建 BusinessOrganisationDetails class 的实例时它说我需要为 IllegalCustomerIDException class 做一个 try catch。鉴于我的情况,我不确定最好的方法
你能更好地解释一下这个场景吗?我不明白您要测试什么以及您到底担心什么。无论如何,根据我的理解,我会说测试中的 try catch 块不是问题,您还可以使用它来测试是否正确抛出异常,例如,如果将错误的客户 ID 作为参数传递给构造函数.或者,如果您只是假设在您的测试中不应抛出异常,您可以在测试方法签名中插入 throws IllegalCustomerIDException
。
try/catch测试没有问题。有两种方法可以测试您的代码是否无异常完成:
@Test
public void testToString() throws IllegalCustomerIDException {
System.out.println("toString");
Address add = new Address("Blackthorn","Kings Lynn","PE30");
BusinessOrganisationDetails instance = new BusinessOrganisationDetails("PEA-1234",
"Smith",add,10,"EA",12);
String expResult = "";
String result = instance.toString();
assertEquals(expResult, result);
}
@Test
public void testToString() {
System.out.println("toString");
try {
Address add = new Address("Blackthorn","Kings Lynn","PE30");
BusinessOrganisationDetails instance = new BusinessOrganisationDetails("PEA-1234",
"Smith",add,10,"EA",12);
String expResult = "";
String result = instance.toString();
assertEquals(expResult, result);
} catch(BusinessOrganisationDetails e) {
fail(e.getMessage);
}
}
如果不想测试是否抛出异常,可以使用 Rule
和 ExcpectedException
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void testToString() throws IllegalCustomerIDException {
System.out.println("toString");
exception.expect(IllegalCustomerIDException.class);
exception.expectMessage("unkwon customer id 4711");
Address add = new Address("Blackthorn","Kings Lynn","PE30");
BusinessOrganisationDetails instance = new BusinessOrganisationDetails("PEA-1234",
"Smith",add,10,"EA",12);
}