2017-11-21 14 views
1

ここには人物オブジェクトを保持するための人物クラスがあります。それは比較のためのComparable実装を持っています。ここでRxJava 2のTestObserverを使ってオブジェクトのリストを単体テストする方法は?

public class Person implements Comparable<Person> { 

    private String firstName = ""; 
    private String lastName = ""; 

    public Person(String firstName, String lastName) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
    } 

    @Override 
    public int compareTo(@NonNull Person person) { 
     return this.firstName.compareTo(person.getFirstName()); 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 
} 

は、人々のリストや人々のリストを作成するためのファクトリクラスがを観測です。常に同じセットの人を作成します。

import java.util.ArrayList; 
import io.reactivex.Observable; 

public class PeopleFactory { 

    public static ArrayList<Person> createPeople() { 
     ArrayList<Person> people = new ArrayList<>(); 

     for (int i=0; i<10; i++) { 
      people.add(new Person("Thanks" + i, "Giving" + i)); 
     } 

     return people; 
    } 

    public static Observable<ArrayList<Person>> createPersonObservable() { 
     return Observable.just(createPeople()); 
    } 
} 

は今、私は観察可能な人から発せられた人々からの人々のリストに対する人々のリストをテストするためにRxJava 2でTestObserverを使用します。

import org.junit.Test; 
import java.util.ArrayList; 
import io.reactivex.Observable; 
import io.reactivex.observers.TestObserver; 


public class PersonTest { 
    @Test 
    public void testPeople() { 
     // Creating a people Observable 
     Observable<ArrayList<Person>> peopleObservable = PeopleFactory.createPersonObservable(); 

     // Creating an test observer and subscribe to the above peopleObservable 
     TestObserver<ArrayList<Person>> observer = new TestObserver<>(); 
     peopleObservable.subscribe(observer); 

     // Creating the same people list and assert this people list with the people list from the peopleObservable 
     ArrayList<Person> people = PeopleFactory.createPeople(); 
     observer.assertValue(people); 
    } 
} 

私はPeopleFactoryが観察普通の人のリストのための人々の同じセットと人々のリストを作成しているため、このテストに合格する必要があり期待していて、Personクラスは、オブジェクトがのように等しくなりますと言うどの匹敵ありfirstNameが等しい限り長くします。正しい方法や、どのように私のユニットテストすることができますが何であるかを

java.lang.AssertionError: Expected: [[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]] (class: ArrayList), Actual: [[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]] (class: ArrayList) (latch = 0, values = 1, errors = 0, completions = 1) 

    at io.reactivex.observers.BaseTestConsumer.fail(BaseTestConsumer.java:163) 
    at io.reactivex.observers.BaseTestConsumer.assertValue(BaseTestConsumer.java:332) 
    at com.example.myapplication.PersonTest.testPeople(PersonTest.java:21) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) 
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131) 

ので、

しかし、この同等を無視対象アドレスに対してそれを比較し、私の次のテスト失敗メッセージを与えたテストRxJava 2でTestObserverを使っているオブジェクトのリスト?

注:上記のコードはすべてAndroidスタジオで書かれています。 Comparableのjavadocツールから

答えて

2

このインタフェースを実装する各クラスのオブジェクトに全体順序付けを強制します。 ... 厳密には(x.compareTo(y)== 0)==(x.equals(y))は必須ではありません。

Comparable::compareToの方法は、要素の順序をソートしたり、他の方法で比較する場合にのみ使用されます。平等は完全に別個であり、あなたが指摘したように、オブジェクトアイデンティティがアサートされています。

あなたはすぐにフレームワークがそう、それはあなたがテストしてPersonクラスのオーバーロードされていないObject::equalsを呼び出しているソースhttps://github.com/ReactiveX/RxJava/blob/v2.1.6/src/main/java/io/reactivex/observers/BaseTestConsumer.java#L331

if (!ObjectHelper.equals(value, v)) { 
    throw fail("Expected: " + valueAndClass(value) + ", Actual: " + valueAndClass(v)); 
} 

ObjectHelper::equals

public static boolean equals(Object o1, Object o2) { // NOPMD 
    return o1 == o2 || (o1 != null && o1.equals(o2)); 
} 

を見て、何をしているか確認することができます。オブジェクトIDを使用し、期待どおりに失敗します。

0

おかげ@ Murkaの答えに、それは私が@Override public boolean equals(Object obj)

public class Person { 

    private String firstName = ""; 
    private String lastName = ""; 

    public Person(String firstName, String lastName) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
    } 


    @Override 
    public boolean equals(Object obj) { 
     return this.firstName.equalsIgnoreCase(((Person) obj).getFirstName()); 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 
} 
Comparable実装を交換した後、期待通りに動作します
関連する問題