'Writing JUnit tests for restTemplate.delete with Observable throwing OnErrorNotImplementedException
I have two methods in my Spring Boot(version 1.3.3) project as follows;
public Observable<Void> methodA() {
return this.methodB().map(s -> {
methodC.subscribe( aVoid ->
// some code
);
// some code
});
}
private Observable<Void> methodC() {
return Observable<~>create(sub -> {
restTemplate.delete(//some code);
sub.onNext(null);
sub.onCompleted();
}).doOnNext(//some code)
.doOnError(//some code);
}
I am using rxjava version 1.3.8. I am trying to write Junit tests for methodC where restTemplate.delete(//some code); throws a HttpClientErrorException. For that, mocked the restTemplate.delete(//some code); as follows;
@Test
public void test() {
// some code
Mockito.doThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND)).when(restTemplate)
.delete(//some code);
// some code
final Observable<Void> result = methodA();
final TestSubscriber<Void> subscriber = new TestSubscriber();
result.subscribe(subscriber);
subscriber.assertNotCompleted();
subscriber.assertError(HttpClientErrorException.class);
}
The mock is working as expected but it throws rx.exceptions.OnErrorNotImplementedException: 404 NOT_FOUND and fails the test. but when I run the code, it is working as expected.
What am I missing here? How can I mock restTemplate.delete(// some code); to throw HttpClientErrorException and assert it?
Thanks in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|