2016-08-18 12 views
0

私はSerializable/Externalizableのかなりの数のクラスを持っています。残念ながら、それはクラスに追加するときに新しいフィールドを整理/解除することを忘れているか、readDoubleの代わりにreadObjectの誤字を忘れてしまいます。だから私は無視され、実行されないユニットテストのいくつかの種類を書くことにしたmaven-surefire-plugin。それは私の必要性のためです。それは次のようになります。2つのオブジェクトをフィールドでどのように比較するのですか?

@Test(enabled = false) 
public void testEquality(Object o){ 
    String oPart = o.toString(); 
    try (FileOutputStream fos = new FileOutputStream("/home/myusr/testser/" + oPart); 
     ObjectOutputStream ous = new ObjectOutputStream(fos)) { 
     ous.writeObject(o); 
    } catch (FileNotFoundException | IOException e) { 
     e.printStackTrace(); 
    } 
    Object oo = null; 
    try (FileInputStream fis = new FileInputStream("/home/myusr/testser/" + oPart); 
     ObjectInputStream ois = new ObjectInputStream(fis)) { 
     Object oo = ois.readObject(); 
    } catch (FileNotFoundException | IOException | ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 
    // Need to compare all non-transient fields of o and oo 
} 

しかし、問題は、同じ名前を持つtransientていないすべてのフィールドを比較する方法です。私はequalsメソッドをテスト目的のためだけにオーバーライドしたくありません。

これに対処する方法はありますか?多分それに対処できるいくつかのcommons/guavaライブラリがあります。

+0

あなたは[shazamcrest](https://github.com/shazam/shazamcrest)を見ましたか? –

答えて

2

YourClaas.class.getDeclaredFields()で始まるリフレクションを使用できます。次に、返されたフィールドをフィルタリングし、オブジェクトから値を取得して比較を行う必要があります。

関連する問題