diff --git a/rxjava-modules/rxjava-core-2/README.md b/rxjava-modules/rxjava-core-2/README.md new file mode 100644 index 0000000000..72c9fba62c --- /dev/null +++ b/rxjava-modules/rxjava-core-2/README.md @@ -0,0 +1,5 @@ +## RxJava + +This module contains articles about RxJava. + +### Relevant articles: diff --git a/rxjava-modules/rxjava-core-2/pom.xml b/rxjava-modules/rxjava-core-2/pom.xml new file mode 100644 index 0000000000..ccadf38ba3 --- /dev/null +++ b/rxjava-modules/rxjava-core-2/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + rxjava-core-2 + 1.0-SNAPSHOT + rxjava-core-2 + + + com.baeldung.rxjava-modules + rxjava-modules + 0.0.1-SNAPSHOT + + + \ No newline at end of file diff --git a/rxjava-modules/rxjava-core-2/src/main/java/com/baeldung/rxjava/justvscallable/EmployeeRepository.java b/rxjava-modules/rxjava-core-2/src/main/java/com/baeldung/rxjava/justvscallable/EmployeeRepository.java new file mode 100644 index 0000000000..03d95e2c8a --- /dev/null +++ b/rxjava-modules/rxjava-core-2/src/main/java/com/baeldung/rxjava/justvscallable/EmployeeRepository.java @@ -0,0 +1,5 @@ +package com.baeldung.rxjava.justvscallable; + +public interface EmployeeRepository { + String findById(Long id); +} diff --git a/rxjava-modules/rxjava-core-2/src/main/resources/logback.xml b/rxjava-modules/rxjava-core-2/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/rxjava-modules/rxjava-core-2/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/rxjava-modules/rxjava-core-2/src/test/java/com/baeldung/rxjava/justvscallable/EmployeeRepositoryTest.java b/rxjava-modules/rxjava-core-2/src/test/java/com/baeldung/rxjava/justvscallable/EmployeeRepositoryTest.java new file mode 100644 index 0000000000..0b1f98d50f --- /dev/null +++ b/rxjava-modules/rxjava-core-2/src/test/java/com/baeldung/rxjava/justvscallable/EmployeeRepositoryTest.java @@ -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 employee = Single.just(repository.findById(123L)); + + Mockito.verify(repository, times(1)) + .findById(123L); + } + + @Test + void givenASubscriber_whenUsingJust_thenReturnTheCorrectValue() { + TestSubscriber testSubscriber = new TestSubscriber<>(); + Mockito.when(repository.findById(123L)) + .thenReturn("John Doe"); + + Single employee = Single.just(repository.findById(123L)); + employee.subscribe(testSubscriber); + + testSubscriber.assertValue("John Doe"); + testSubscriber.assertCompleted(); + } + + @Test + void givenNoSubscriber_whenUsingFromCallable_thenNoDataIsFetched() { + Single employee = Single.fromCallable(() -> repository.findById(123L)); + + Mockito.verify(repository, never()) + .findById(123L); + } + + @Test + void givenASubscriber_whenUsingFromCallable_thenReturnCorrectValue() { + TestSubscriber testSubscriber = new TestSubscriber<>(); + Mockito.when(repository.findById(123L)) + .thenReturn("John Doe"); + + Single employee = Single.fromCallable(() -> repository.findById(123L)); + employee.subscribe(testSubscriber); + + Mockito.verify(repository, times(1)) + .findById(123L); + testSubscriber.assertCompleted(); + testSubscriber.assertValue("John Doe"); + } +} \ No newline at end of file