這是我的班級結(jié)構(gòu)public class OrderRuntimeException extends RuntimeException{ private final Field field; public OrderManagementServiceRuntimeException(String messege, MyError myError) { super(messege); this.field = field; }}public Class MyError{ String name; Sttring description; public MyError(String name, String description){ this.name = name; this.description = description; } }public void getOrders(Order order){ ....... throw new OrderRuntimeException("Invalid Order",new MyError("Illegale","this is an illegal..."))}我想用 Junit 測試這個(gè)異常和 MeError 類型的對象(名稱和描述)。我寫了這個(gè)public class MyTest { @Rule public ExpectedException thrown = ExpectedException.none();@Test(expectedExceptions = OrderRuntimeException.class, expectedExceptionsMessageRegExp = "Invalid Order") public void testSetRequestTime_invalidRequestDTOImplType() { thrown.expect(......); }}我使用 @Rule .. 來獲取 throw.expect() 功能。但是我無法從這個(gè)測試中得到 MyError 類型的對象。請發(fā)表評論或回答,如果有人對解決方案有想法。
1 回答

蕭十郎
TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊
我認(rèn)為你可以org.hamcrest.Matchers用來做出這種類型的斷言。
expectedException.expect(OrderRuntimeException.class);
expectedException.expect(org.hamcrest.Matchers.hasProperty("myError", allOf(
org.hamcrest.Matchers.hasProperty("name", is("exampleName")),
org.hamcrest.Matchers.hasProperty("description", is("exampleDescription"))
)));
添加回答
舉報(bào)
0/150
提交
取消