RxJava file.createNewFile () always returns TRUE

I am trying to create a new file using RxJava on Android, for example:

public Observable<Boolean> createRx(String name) {

    return Observable.just(name)
        .map(new Func1<String, Boolean>() {
            @Override
            public Boolean call(String s) {
                File newFile = new File(localPath + "/" + s);

                try {
                    return newFile.createNewFile();
                } catch (IOException e) {
                    throw Exceptions.propagate(e);
                }
            }
        });
}

To create a new file in normal mode, follow these steps:

public boolean createNonRx(String name) {

    boolean ret = false;

    try {
        File newFile = new File(localPath + "/" + name);
        ret = newFile.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return ret;
}

My code is JUnitTest and TestReport:

@Test
public void testCreateRx() throws Exception { // FAIL!
    TestSubscriber<Boolean> testSubscriber = new TestSubscriber<>();

    source.createRx("JunitTest_Create_Rx").subscribe(testSubscriber);
    testSubscriber.assertNoErrors();
    testSubscriber.assertReceivedOnNext(Arrays.asList(Boolean.TRUE)); //PASS!

    source.createRx("JunitTest_Create_Rx").subscribe(testSubscriber);
    testSubscriber.assertNoErrors();
    testSubscriber.assertReceivedOnNext(Arrays.asList(Boolean.FALSE)); //FAIL!
    //expected to be [false] (Boolean) but was: [true] (Boolean)
}


@Test
public void testCreateNonRx() {  // PASS!
    boolean fstRet = source.createNonRx("JunitTest_LocalDataSource_Create_Non_Rx");
    assertTrue(fstRet);

    boolean secRet = source.createNonRx("JunitTest_LocalDataSource_Create_Non_Rx");
    assertFalse(secRet);
}

I'm new to RxJava, is there a problem with my code?

Why does the createRx () call return TRUE when trying to create an existing file?

Thanks for any help.

+4
source share
1 answer

Subscribersand Observersshould not be reused - sign up every time with a new one and tell us what you received

+1
source

Source: https://habr.com/ru/post/1660028/


All Articles