Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在RXJava2中使用TestObserver对对象列表进行单元测试?_Java_Unit Testing_Observable_Junit4_Rx Java2 - Fatal编程技术网

如何在RXJava2中使用TestObserver对对象列表进行单元测试?

如何在RXJava2中使用TestObserver对对象列表进行单元测试?,java,unit-testing,observable,junit4,rx-java2,Java,Unit Testing,Observable,Junit4,Rx Java2,这里有一个用于保存Person对象的Person类。它有可比较的实现来进行比较 public class Person implements Comparable<Person> { private String firstName = ""; private String lastName = ""; public Person(String firstName, String lastName) { this.firstName = f

这里有一个用于保存Person对象的Person类。它有可比较的实现来进行比较

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;
    }
}
因此,在RxJava 2中使用TestObserver对对象列表进行单元测试的正确方法是什么?或者如何进行单元测试?


注意:以上所有代码都是在Android Studio中编写的。

来自于javadoc的
Comparable

此接口对实现它的每个类的对象施加总排序。。。 强烈建议(x.compareTo(y)==0)=(x.equals(y)),但不是严格要求

Comparable::compareTo
方法仅在排序或比较元素顺序时使用。相等是完全独立的,正如您所注意到的,对象标识被断言

通过查看源代码,可以快速确认框架正在做什么

ObjectHelper::equals

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

因此,它调用了
Object::equals
,您没有为
Person
类重载它,因此测试使用对象标识,并按预期失败。

多亏了@Murka的回答,在我用
@Override public boolean equals(Object obj)
替换了
可比的
实现后,它的效果与预期一样好。

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);
    }
}
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)
if (!ObjectHelper.equals(value, v)) {
  throw fail("Expected: " + valueAndClass(value) + ", Actual: " + valueAndClass(v));
}
public static boolean equals(Object o1, Object o2) { // NOPMD
  return o1 == o2 || (o1 != null && o1.equals(o2));
}
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;
    }
}