BAEL-5908: Single.just vs fromCallable
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.rxjava.justvscallable;
|
||||
|
||||
public interface EmployeeRepository {
|
||||
String findById(Long id);
|
||||
}
|
||||
13
rxjava-modules/rxjava-core-2/src/main/resources/logback.xml
Normal file
13
rxjava-modules/rxjava-core-2/src/main/resources/logback.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.rxjava.justvscallable;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.times;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import rx.Single;
|
||||
import rx.observers.TestSubscriber;
|
||||
|
||||
class EmployeeRepositoryTest {
|
||||
|
||||
public EmployeeRepository repository = mock(EmployeeRepository.class);
|
||||
|
||||
@BeforeEach
|
||||
public void beforeEach() {
|
||||
reset(repository);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNoSubscriber_whenUsingJust_thenDataIsFetched() {
|
||||
Mockito.when(repository.findById(123L))
|
||||
.thenReturn("John Doe");
|
||||
|
||||
Single<String> employee = Single.just(repository.findById(123L));
|
||||
|
||||
Mockito.verify(repository, times(1))
|
||||
.findById(123L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenASubscriber_whenUsingJust_thenReturnTheCorrectValue() {
|
||||
TestSubscriber<String> testSubscriber = new TestSubscriber<>();
|
||||
Mockito.when(repository.findById(123L))
|
||||
.thenReturn("John Doe");
|
||||
|
||||
Single<String> employee = Single.just(repository.findById(123L));
|
||||
employee.subscribe(testSubscriber);
|
||||
|
||||
testSubscriber.assertValue("John Doe");
|
||||
testSubscriber.assertCompleted();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNoSubscriber_whenUsingFromCallable_thenNoDataIsFetched() {
|
||||
Single<String> employee = Single.fromCallable(() -> repository.findById(123L));
|
||||
|
||||
Mockito.verify(repository, never())
|
||||
.findById(123L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenASubscriber_whenUsingFromCallable_thenReturnCorrectValue() {
|
||||
TestSubscriber<String> testSubscriber = new TestSubscriber<>();
|
||||
Mockito.when(repository.findById(123L))
|
||||
.thenReturn("John Doe");
|
||||
|
||||
Single<String> employee = Single.fromCallable(() -> repository.findById(123L));
|
||||
employee.subscribe(testSubscriber);
|
||||
|
||||
Mockito.verify(repository, times(1))
|
||||
.findById(123L);
|
||||
testSubscriber.assertCompleted();
|
||||
testSubscriber.assertValue("John Doe");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user