我正在爲使用Cucumber(-JVM),Junit,Java 8接受XML文件(具有多達數百個字段)的應用程序創建驗收測試框架。創建序列化的POJO到XML了很多使用生成器接口的類庫(以避免幾十建設者,並有各種各樣的聲明API),即:將Cucumber場景DataTable(HashMap)轉換爲方法調用
new Form.Builder
.setThisField(withThis)
.setThatField(withThat)
.build();
我做了這個庫,因爲我想在我的測試中動態生成有效的XML文件。我希望我的黃瓜情景閱讀喜歡英語,所以我決定用的,而不是寫類似數據表:
Given I have a form with x field
and with y field
and with z field
and ...
用50種不同的「和」線。因此,他們是這樣的:
Given that I have Form B101 with the following fields:
| X Field | X Value |
| Y Field | Y Value |
| Z Field | Z Value |
問題:
我想在黃瓜數據表中的鍵(也就是變成一個HashMap)映射到方法名我的建造者模式。起初我以爲使用lambda表達式和方法引用可能會讓我完成這個任務,但我還沒有找到方法。
所以我的下一個想法是反思。我決定從在屬性黃瓜數據表密鑰的方法名存儲映射文件,如:
//mapping.properties
X\ Field = setXField
// the \ after X is to escape the space
我遇到了一個問題,但:有的在黃瓜數據表的字段映射到深層嵌套字段(由於XML模式)在我的XML數據綁定庫中。例如:
「X字段」嵌套在A字段中。所以在我的測試方法,我需要做的:
AField aField = new AField(new XField());
但隨着Java反射,你需要知道的事實之前,參數的數據類型(或因此我認爲)。例如,如果我想找出什麼參數類型與方法有關:
Class[] paramString = new Class[1];
paramString[0] = AField.class;
// So I need to know before the fact that methodName (below) has a parameter
// of type AField in order to .... get the parameter types of methodName.
// this is legal because a method with methodName exists and it takes
// one parameter with type AField.
Class[] parameterTypes =
formB.getClass().getMethod(methodName, paramString).getParameterTypes();
// this is not legal. no method named methodName which doesn't
// take parameters exists
Class[] parameterTypes =
formB.getClass().getMethod(methodName).getParameterTypes();
我相信我能找到一種解決方法,但最終好像我要下去了「哈克'路徑。有沒有更好的方法來解決這個問題?還是我在「正確」的道路上?
你講一個Ø很多關於您的實現。你能分享更多關於你的問題嗎?我感覺你以一種不擅長的方式使用黃瓜。但是一個具體的例子可能會更好地理解你的目標。 –
@ThomasSundberg按問題你是指我正在測試的應用程序?基本上我正在測試一個將接受XML文件的Web服務。我構建了一個工具來動態生成xml文件。其中一些XML文件有125多個字段,我需要測試的業務規則有25個不同的字段。所以我需要在黃瓜的每個XML文件中表達我想要的數據。我認爲數據表是最明智的解決方案,而不是a)在黃瓜場景中有100行b)表示100個字段值的哈希短手。 – douglas
@ThomasSundberg此外,我無法更改應用程序以將XML文件分解爲更小的子XML文件。 – douglas