遅くなってすみません。同様のニーズがあるため、リファクタリング後の特定のクラスへの時間の経過に伴う変化を示すプレゼンテーションとトレーニングを多数作成しました。したがって、テストは同じですが、クラス自体が異なります。各バージョンは異なるパッケージに含まれています。クラス名はそのままです。これは、OPが直面していたものと似ています。
パラメータ化されたソリューションは扱いにくいと思います。私は2つのソリューションを使用しました。どちらも Groovy で書かれていますが、以下の 2 番目の例は純粋な Java ソリューションとして実装できるはずです。
最初は次のとおりです。
public class ItemTest {
def defaultConstructedClasses = [
new com.groovifyingjava.essentialgroovification.Item()
,new com.groovifyingjava.rewriteequals.Item()
,new com.groovifyingjava.removesemicolons.Item()
,new com.groovifyingjava.removeparentheses.Item()
,new com.groovifyingjava.removeaccessors.Item()
,new com.groovifyingjava.removeconstructors.Item()
,new com.groovifyingjava.removeimports.Item()
,new com.groovifyingjava.removereturn.Item()
,new com.groovifyingjava.clarifyidentityequality.Item()
,new com.groovifyingjava.coercetypes.Item()
,new com.groovifyingjava.defaultaccessmodifiers.Item()
,new com.groovifyingjava.eachiteration.Item()
,new com.groovifyingjava.fieldaccessnotation.Item()
,new com.groovifyingjava.namedparameters.Item()
,new com.groovifyingjava.optionaldatatyping.Item()
,new com.groovifyingjava.safelynavigate.Item()
,new com.groovifyingjava.simplifylistmapsetcreation.Item()
,new com.groovifyingjava.stringinterpolation.Item()
,new com.groovifyingjava.useelvisoperator.Item()
,new com.groovifyingjava.useequalsoperator.Item()
,new com.groovifyingjava.usemathoperators.Item()
,new com.groovifyingjava.useprintln.Item()
]
def overloadedConstructorArgs = [itemId: "14-101", manufacturer: "Raleigh", model: "Superbe Roadster", cost: 179.89, quantityOnHand: 10]
def overloadedConstructedClasses = [
new com.groovifyingjava.essentialgroovification.Item(overloadedConstructorArgs)
,new com.groovifyingjava.rewriteequals.Item(overloadedConstructorArgs)
,new com.groovifyingjava.removesemicolons.Item(overloadedConstructorArgs)
,new com.groovifyingjava.removeparentheses.Item(overloadedConstructorArgs)
,new com.groovifyingjava.removeaccessors.Item(overloadedConstructorArgs)
,new com.groovifyingjava.removeconstructors.Item(overloadedConstructorArgs)
,new com.groovifyingjava.removeimports.Item(overloadedConstructorArgs)
,new com.groovifyingjava.removereturn.Item(overloadedConstructorArgs)
,new com.groovifyingjava.clarifyidentityequality.Item(overloadedConstructorArgs)
,new com.groovifyingjava.coercetypes.Item(overloadedConstructorArgs)
,new com.groovifyingjava.defaultaccessmodifiers.Item(overloadedConstructorArgs)
,new com.groovifyingjava.eachiteration.Item(overloadedConstructorArgs)
,new com.groovifyingjava.fieldaccessnotation.Item(overloadedConstructorArgs)
,new com.groovifyingjava.namedparameters.Item(overloadedConstructorArgs)
,new com.groovifyingjava.optionaldatatyping.Item(overloadedConstructorArgs)
,new com.groovifyingjava.safelynavigate.Item(overloadedConstructorArgs)
,new com.groovifyingjava.simplifylistmapsetcreation.Item(overloadedConstructorArgs)
,new com.groovifyingjava.stringinterpolation.Item(overloadedConstructorArgs)
,new com.groovifyingjava.useelvisoperator.Item(overloadedConstructorArgs)
,new com.groovifyingjava.useequalsoperator.Item(overloadedConstructorArgs)
,new com.groovifyingjava.usemathoperators.Item(overloadedConstructorArgs)
,new com.groovifyingjava.useprintln.Item(overloadedConstructorArgs)
]
@Test public void canCreateDefaultInstance() {
for(def item in defaultConstructedClasses) {
assertNull "Default construction of class ${item.class.name} failed to properly initialize field.", item.itemId
assertNull "Default construction of class ${item.class.name} failed to properly initialize field.", item.manufacturer
assertNull "Default construction of class ${item.class.name} failed to properly initialize field.", item.model
assertNull "Default construction of class ${item.class.name} failed to properly initialize field.", item.cost
assertEquals "Default construction of class ${item.class.name} failed to properly initialize field.", 0, item.quantityOnHand
assertEquals "Default construction of class ${item.class.name} failed to properly initialize field.", BigDecimal.ZERO, item.inventoryValue as BigDecimal
}
}
@Test public void canCreateInstancesFromOverloadedConstructor() {
for(def item in overloadedConstructedClasses) {
assertEquals "Instantiation of class ${item.class.name} using overloaded constructor failed to properly initialize field.","14-101", item.itemId
assertEquals "Instantiation of class ${item.class.name} using overloaded constructor failed to properly initialize field.", "Raleigh", item.manufacturer
assertEquals "Instantiation of class ${item.class.name} using overloaded constructor failed to properly initialize field.", "Superbe Roadster", item.model
assertEquals "Instantiation of class ${item.class.name} using overloaded constructor failed to properly initialize field.", 179.89, item.cost
assertEquals "Instantiation of class ${item.class.name} using overloaded constructor failed to properly initialize field.", 10, item.quantityOnHand
assertEquals "Instantiation of class ${item.class.name} using overloaded constructor failed to properly initialize field.", new BigDecimal("1798.90"), item.inventoryValue as BigDecimal
}
}
}
これはうまく機能し、実装が非常に簡単です。Groovy ダック タイピングを使用して各クラスをテストします。欠点は、Eclipse の junit コンソールが最後のテスト実行を表示することです。これはおそらく大したことではありません。また、失敗したテストで実行を停止します。アサート メッセージ文字列にクラス名が含まれていることに注意してください。これにより、失敗したクラスを簡単に特定できます。
2 番目のバージョンでは、抽象テスト クラスが必要であり、テスト対象のクラスの各パッケージに対応する各パッケージにテスト クラスがあります。これらはスタブにすぎませんが、テストを慎重に、またはスイート内で実行できます。
public abstract class CommonItemTest {
def overloadedConstructorArgs = [itemId: "14-101", manufacturer: "Raleigh", model: "Superbe Roadster", cost: 179.89, quantityOnHand: 10]
@Test public void canCreateDefaultInstance() {
def item = defaultConstructedClass
assertNull "Default construction of class ${item.class.name} failed to properly initialize field. Expected itemId to be null.", item.itemId
assertNull "Default construction of class ${item.class.name} failed to properly initialize field. Expected manufacturer to be null.", item.manufacturer
assertNull "Default construction of class ${item.class.name} failed to properly initialize field. Expected model to be null.", item.model
assertNull "Default construction of class ${item.class.name} failed to properly initialize field. Expected cost to be null.", item.cost
assertEquals "Default construction of class ${item.class.name} failed to properly initialize field. Expected quantityOnHand to be zero.", 0, item.quantityOnHand
assertEquals "Default construction of class ${item.class.name} failed to properly initialize field. Expected inventoryValue to be zero.", BigDecimal.ZERO, item.inventoryValue as BigDecimal
}
@Test public void canCreateInstancesFromOverloadedConstructor() {
def item = overloadedConstructedClass
assertEquals "Instantiation of class ${item.class.name} using overloaded constructor failed to properly initialize field.","14-101", item.itemId
assertEquals "Instantiation of class ${item.class.name} using overloaded constructor failed to properly initialize field.", "Raleigh", item.manufacturer
assertEquals "Instantiation of class ${item.class.name} using overloaded constructor failed to properly initialize field.", "Superbe Roadster", item.model
assertEquals "Instantiation of class ${item.class.name} using overloaded constructor failed to properly initialize field.", 179.89, item.cost
assertEquals "Instantiation of class ${item.class.name} using overloaded constructor failed to properly initialize field.", 10, item.quantityOnHand
assertEquals "Instantiation of class ${item.class.name} using overloaded constructor failed to properly initialize field.", new BigDecimal("1798.90"), item.inventoryValue as BigDecimal
}
abstract Object getDefaultConstructedClass();
abstract Object getOverloadedConstructedClass();
}
そして実装…
public class ItemTest extends CommonItemTest {
public Object getDefaultConstructedClass() {
return new Item()
}
public Object getOverloadedConstructedClass() {
return new Item(overloadedConstructorArgs)
}
}