簡體   English   中英

如何在RxJava 2中使用TestObserver對對象列表進行單元測試?

[英]How to unit test a list of objects using TestObserver in RxJava 2?

這是用於保存人員對象的人員類 它具有Comparable實現以進行比較。

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;
    }
}

這是一個用於創建人員列表和人員列表Observable工廠類 它總是創建同一組人。

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來針對人員列表與人員Observable發出的人員列表進行測試

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正在為正常人員列表和人員列表Observable創建相同的人員集合,並且Person類具有Comparable,該對象表示只要firstName相等,對象就將相等。

但是測試忽略了此Comparable,將其與對象地址進行了比較 ,並給出了以下測試失敗消息:

java.lang.AssertionError: Expected: [com.example.myapplication.Person@7006c658, com.example.myapplication.Person@34033bd0, com.example.myapplication.Person@47fd17e3, com.example.myapplication.Person@7cdbc5d3, com.example.myapplication.Person@3aa9e816, com.example.myapplication.Person@17d99928, com.example.myapplication.Person@3834d63f, com.example.myapplication.Person@1ae369b7, com.example.myapplication.Person@6fffcba5, com.example.myapplication.Person@34340fab] (class: ArrayList), Actual: [com.example.myapplication.Person@2aafb23c, com.example.myapplication.Person@2b80d80f, com.example.myapplication.Person@3ab39c39, com.example.myapplication.Person@2eee9593, com.example.myapplication.Person@7907ec20, com.example.myapplication.Person@546a03af, com.example.myapplication.Person@721e0f4f, com.example.myapplication.Person@28864e92, com.example.myapplication.Person@6ea6d14e, com.example.myapplication.Person@6ad5c04e] (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 Studio編寫的。

Comparable的javadoc中:

該接口對實現該接口的每個類的對象強加了總體排序。 ...強烈建議(但並非嚴格要求(x.compareTo(y)== 0)==(x.equals(y))。

Comparable::compareTo方法僅在排序或比較元素順序時使用。 平等是完全獨立的,並且正如您所指出的,對象身份被斷言。

您可以通過查看源代碼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));
}

因此,它正在調用Object::equals ,而您沒有為Person類重載它,因此測試使用對象標識,並且按預期失敗。

感謝@ Murka的答案,它按預期工作后,我更換了Comparable實現與@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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM