2013-07-30 123 views
8

幾天來,我正在使用Junit的Matchers功能。一切工作正常,但我正在尋找一個匹配器,它使用比較器進行比較,並且不依賴於對象等於methodes。比較者的Junit Matcher?

我想更換

Assert.assertThat(one, CoreMatchers.equalTo(two) 

喜歡的東西(僞)

Assert.assertThat(eins, CoreMatchers.equalTo(operand, new MyComparator()) 

你知道是否存在一個簡單的開箱即用的解決方案嗎?我沒有在谷歌找到一個,也不想寫一個。

+2

你可以試着寫自己的匹配,延長[BaseMatcher](http://junit.org/javadoc/4.9/組織/ hamcrest/BaseMatcher.html)。 –

+1

驚訝它不存在。請發佈您的解決方案,以便下一個人可以使用它。 –

回答

0

我不知道在Hamcrest做任何事情。您可能需要編寫自定義匹配器。有一件事要考慮:如果equals不返回true,對象是否相等?如果您正在測試特定屬性,則您的自定義匹配器可能會更清晰,如FeatureMatcherIs there a simple way to match a field using Hamcrest?)。例如,如果測試是一個預期的標籤:

assertThat(eins, equalToUnderComparison("1", new LabelComparator()); 

可以更清楚的:

assertThat(eins, hasLabel(eq("1"))); 

編寫自定義的匹配並不需要很多的代碼(Writing custom matchers),而且也沒有理由避免它,如果它使代碼更具可讀性。

+0

對於JPA模型實例而言,您無法實現基於業務邏輯的equals/hashCode。請參閱https://stackoverflow.com/questions/5031614/the-jpa-hashcode-equals-dilemma – GKislin

10

這現在在Hamcrest 2.0.0.0+中支持。

可以使用org.hamcrest.comparator.ComparatorMatcherBuilder類來實現這一點,例如:

ComparatorMatcherBuilder builder = ComparatorMatcherBuilder.comparedBy(equivalenceComparator); 
Assert.assertThat(eins, builder.comparesEqualTo(operand)); 
1

我hamcrest 1.3有同樣的問題,並通過編寫匹配,跟隨ISEQUAL匹配器的代碼解決,但使用給定的比較器而不是Object#equals()。

import org.hamcrest.BaseMatcher; 
import org.hamcrest.Description; 
import org.hamcrest.Factory; 
import org.hamcrest.Matcher; 
import org.junit.Assert; 

import java.lang.reflect.Array; 
import java.util.Comparator; 

/** 
* Is the value equal to another value, as tested by the 
* given Comparator?<br/> 
* Based on the example of {@link org.hamcrest.core.IsEqual}. 
* 
* @author Serhat Cinar 
*/ 
public class IsEqualWithComparator<T> extends BaseMatcher<T> { 
    private final Object expectedValue; 
    private final Comparator<T> comparator; 

    public IsEqualWithComparator(T equalArg, Comparator<T> comparator) { 
     expectedValue = equalArg; 
     this.comparator = comparator; 
    } 

    @Override 
    public boolean matches(Object actualValue) { 
     return areEqual(actualValue, expectedValue, comparator); 
    } 

    @Override 
    public void describeTo(Description description) { 
     description.appendValue(expectedValue); 
    } 

    private static boolean areEqual(Object actual, Object expected, Comparator comparator) { 
     if (actual == null) { 
      return expected == null; 
     } 

     if (expected != null && isArray(actual)) { 
      return isArray(expected) && areArraysEqual(actual, expected, comparator); 
     } 

     return comparator.compare(actual, expected) == 0; 
    } 

    private static boolean areArraysEqual(Object actualArray, Object expectedArray, Comparator comparator) { 
     return areArrayLengthsEqual(actualArray, expectedArray) && areArrayElementsEqual(actualArray, expectedArray, comparator); 
    } 

    private static boolean areArrayLengthsEqual(Object actualArray, Object expectedArray) { 
     return Array.getLength(actualArray) == Array.getLength(expectedArray); 
    } 

    private static boolean areArrayElementsEqual(Object actualArray, Object expectedArray, Comparator comparator) { 
     for (int i = 0; i < Array.getLength(actualArray); i++) { 
      if (!areEqual(Array.get(actualArray, i), Array.get(expectedArray, i), comparator)) { 
       return false; 
      } 
     } 
     return true; 
    } 

    private static boolean isArray(Object o) { 
     return o.getClass().isArray(); 
    } 

    @Factory 
    public static <T> Matcher<T> equalTo(T operand, Comparator<T> comparator) { 
     return new IsEqualWithComparator<>(operand, comparator); 
    } 

    public static void main(String argv[]) { 
     Assert.assertThat("abc", IsEqualWithComparator.equalTo("ABC", new Comparator<String>() { 
      @Override 
      public int compare(String o1, String o2) { 
       return o1.equalsIgnoreCase(o2) ? 0 : -1; 
      } 
     })); 
    } 
} 
0

另一個選項是使用AspectJ framework custom comparison strategy作爲對象和迭代。

assertThat(frodo).usingComparator(raceComparator).isEqualTo(sam); 
assertThat(fellowshipOfTheRing).usingElementComparator(raceComparator).contains(sauron); 

它也有Field by field comparisons,例如, isEqualToComparingOnlyGivenFieldsisEqualToIgnoringGivenFields比較器。

assertThat(frodo).isEqualToComparingOnlyGivenFields(sam, "race.name"); 
assertThat(frodo).isEqualToIgnoringGivenFields(sam, "name", "age"); 

因此,在大多數情況下,你可以處理斷言沒有定義比較策略