mockito

JUnit5 test with LiveData doesn't execute subscriber's callback

Deadly 提交于 2020-07-06 17:48:34
问题 Background: I have a simple application that fetches movie list using rests API call. The project structure is given below, Activity -> ViewModel -> Repository -> ApiService (Retrofit Interface) The activity subscribes to a LiveData and listens for events changes The ViewModel hosts the MediatorLiveData observed by the activity. Initially the ViewModel sets a Resource.loading(..) value in MediatorLiveData . The ViewModel then calls the repository to get the movie list from ApiService The

JUnit5 test with LiveData doesn't execute subscriber's callback

好久不见. 提交于 2020-07-06 17:44:37
问题 Background: I have a simple application that fetches movie list using rests API call. The project structure is given below, Activity -> ViewModel -> Repository -> ApiService (Retrofit Interface) The activity subscribes to a LiveData and listens for events changes The ViewModel hosts the MediatorLiveData observed by the activity. Initially the ViewModel sets a Resource.loading(..) value in MediatorLiveData . The ViewModel then calls the repository to get the movie list from ApiService The

JUnit5 test with LiveData doesn't execute subscriber's callback

可紊 提交于 2020-07-06 17:43:05
问题 Background: I have a simple application that fetches movie list using rests API call. The project structure is given below, Activity -> ViewModel -> Repository -> ApiService (Retrofit Interface) The activity subscribes to a LiveData and listens for events changes The ViewModel hosts the MediatorLiveData observed by the activity. Initially the ViewModel sets a Resource.loading(..) value in MediatorLiveData . The ViewModel then calls the repository to get the movie list from ApiService The

Simulate first call fails, second call succeeds

你。 提交于 2020-07-04 05:42:37
问题 I want to use Mockito to test the (simplified) code below. I don't know how to tell Mockito to fail the first time, then succeed the second time. for(int i = 1; i < 3; i++) { String ret = myMock.doTheCall(); if("Success".equals(ret)) { log.write("success"); } else if ( i < 3 ) { log.write("failed, but I'll try again. attempt: " + i); } else { throw new FailedThreeTimesException(); } } I can setup the success test with: Mockito.when(myMock).doTheCall().thenReturn("Success"); And the failure

Mocking a post request using Mockito raises javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request

邮差的信 提交于 2020-06-29 04:14:30
问题 I'm totally new to Mockito and I need to mock a post request so that I can test a client alone. However, no matter what I do I get the RESTEASY004655: Unable to invoke request exception. This is the simplified version of what I have so far. I have the class TestClassA which sends a post request to an api as follows: import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; import javax.ws.rs.client.Entity; import javax.ws.rs.core

Mocking a post request using Mockito raises javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request

北城余情 提交于 2020-06-29 04:14:07
问题 I'm totally new to Mockito and I need to mock a post request so that I can test a client alone. However, no matter what I do I get the RESTEASY004655: Unable to invoke request exception. This is the simplified version of what I have so far. I have the class TestClassA which sends a post request to an api as follows: import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; import javax.ws.rs.client.Entity; import javax.ws.rs.core

mockito : mock method call with parameters by reflection

懵懂的女人 提交于 2020-06-28 19:19:07
问题 I'm using mockito and developping with java6 and spring. I'm working on a test API for some developpers and I propose a few methods for mocking objects and methods (it's a legacy code...). Now, I want to replace all this things by mockito but I always propose a test API. So, I developped some methods using mockito. I have an old method with two parameters (String). A first parameter is a mocked service id and its method with parameters. And the second parameter is the returned Object. Example

How to mock lambda with mockito in kotlin

痞子三分冷 提交于 2020-06-27 07:26:59
问题 I have a kotlin Android app. There is a function that loads compositions from the backend and returns them to a callback: getCompositons(callback: (Array<Composition>) -> Unit) How can I mock the callback using mockito. So that I then can do something like this: var callback = //mockito mock getCompositons(callback) verify(callback, timeout(10000)).apply() I read that lambda are matched to the java type function and therefore I assume apply could be the method invoked. Maybe I could mock a

How to mock lambda with mockito in kotlin

雨燕双飞 提交于 2020-06-27 07:26:14
问题 I have a kotlin Android app. There is a function that loads compositions from the backend and returns them to a callback: getCompositons(callback: (Array<Composition>) -> Unit) How can I mock the callback using mockito. So that I then can do something like this: var callback = //mockito mock getCompositons(callback) verify(callback, timeout(10000)).apply() I read that lambda are matched to the java type function and therefore I assume apply could be the method invoked. Maybe I could mock a

UnfinishedStubbingException when passing mocked class as an argument in thenThrow

女生的网名这么多〃 提交于 2020-06-27 03:53:09
问题 Can I pass mocked object as an argument to thenThrow() method? I have something like this: public class MyException extends Exception { public MyException(MockedClass mockedClass) { super("My message:" + mockedClass.doSth("foo")); } } public class TestedServiceTest { @Mock MockedClass mockedClass; @Mock AnotherClass anotherClass; @Before public void init() { when(mockedClass.doSth(anyString())).thenAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation)