LambdaInvokerFactory Unsupported Operation: Cannot evaluate - android

I am invoking an AWS Lambda function directly from my Android app. I have the Request class:
private class MyRequest
{
String param;
public SetIdentityRequest() {}
public SetIdentityRequest(String param) {this.param = param;}
public String getParam() { return param; }
public void setParam(String param) { this.param = param; }
}
I don't need any values returned, so I don't have a Response class. I tried making an empty one in case that was causing my problem, but I got a lot more errors.
My interface class:
private interface MyLambda
{
#LambdaFunction
void myLambda(MyRequest req);
}
And finally my code for calling it:
LambdaInvokerFactory factory = new LambdaInvokerFactory(getApplicationContext(), Regions.US_EAST_1, credentialsProvider);
MyLambda lambda = factory.build(MyLambda.class);
lambda.myLambda(new MyRequest(someString));
Now, the lambda does actually go through and execute properly, so this isn't a dealbreaking kind of error; but I've been seeing when I step through it in debug that the line that has factory.build gets an error Method threw 'java.lang.UnsupportedOperationException' exception. Cannot evaluate com.myapp.$Proxy1.toString(). What is this error about? Do I need to worry about it? What do I need to do to stop getting this error?

Related

ArgumentCaptor captures wrong class

SOLVED: Even if the getValue() on the Argument Captor shows you this, it is normal. To be honest I was expecting to see and instance on the OnLoginWithEmailCallback interface here. The problem on my side was related to a method call on mView which was generating a NPE. Works like a charm now.
ORIGINAL PROBLEM:
I am implementing my first unit test using Mockito in my MVP app and I need to mock the behaviour of a callback when the user is logging in. I am using Firebase to handle the authentication.
I followed a very good tutorial from here : https://fernandocejas.com/2014/04/08/unit-testing-asynchronous-methods-with-mockito/.
I am calling method on class under test. This method calls another one on the Auth Presenter which does the actual work
mPresenter.performLoginWithEmail(EMAIL, PASSWORD);
Then I am verifying that an underlining method in the Auth Presenter class was called. I try to capture the callback interface.
verify(mAuthPresenter, times(1)).login(mOnLoginWithEmailCallbackArgumentCaptor.capture(),
eq(EMAIL), eq(PASSWORD));
The problem is that getValue() from the Argument Captor returns an instance of the mPresenter (class under test) instead of the OnLoginWithEmailCallback interface class. Therefore I get an NPE.
mOnLoginWithEmailCallbackArgumentCaptor.getValue().onLoginWithEmailSuccess();
Here is the complete test class:
#RunWith(MockitoJUnitRunner.class)
public class LoginPresenterTest {
private static String EMAIL = "test#rmail.com";
private static String PASSWORD = "1234";
//class under test
private LoginPresenter mPresenter;
#Mock
AuthPresenter mAuthPresenter;
#Captor
private ArgumentCaptor<OnLoginWithEmailCallback> mOnLoginWithEmailCallbackArgumentCaptor;
#Mock
ILoginActivityView mView;
#Before
public void setupLoginPresenter() {
MockitoAnnotations.initMocks(this);
// Get a reference to the class under test
mPresenter = new LoginPresenter(mView, mAuthPresenter);
}
#Test
public void performLoginWithEmail() {
mPresenter.performLoginWithEmail(EMAIL, PASSWORD);
//wanting to have control over the callback object. therefore call capture to then call methods on the interface
verify(mAuthPresenter, times(1)).login(mOnLoginWithEmailCallbackArgumentCaptor.capture(),
eq(EMAIL), eq(PASSWORD));
mOnLoginWithEmailCallbackArgumentCaptor.getValue().onLoginWithEmailSuccess();
InOrder inOrder = Mockito.inOrder(mView);
inOrder.verify(mView).goToMap();
inOrder.verify(mView).hideProgressBar();
}
}
EDIT: This is the call to mAuthPresenter.login:
SOLVED: getLoginActivityView() was causing an NPE
public void performLoginWithEmail(String email, String password) {
mAuthPresenter.login(new OnLoginWithEmailCallback() {
#Override
public void onLoginWithEmailSuccess() {
getLoginActivityView().goToMap();
getLoginActivityView().hideProgressBar();
}
#Override
public void onLoginWithEmailFailed(String error) {
getLoginActivityView().hideProgressBar();
getLoginActivityView().showToast(error);
}
}, email, password);
}
I also tried using using doAnswer from Mockito:
doAnswer(new Answer() {
#Override
public Object answer(InvocationOnMock invocation) throws Throwable {
((OnLoginWithEmailCallback)invocation.getArguments()[0]).onLoginWithEmailSuccess();
return null;
}
}).when(mAuthPresenter).login(
any(OnLoginWithEmailCallback.class, EMAIL, PASSWORD));
Still, invocation.getArguments() return an instance of the class under test (LoginPresenter), so the same problem as before. Can you help me?
The problem was that i got confused by the Argument Captor which returned me an instance of the caller class (LoginPresenter). The problem in my case was with a method inside the anonymouse class OnLoginWithEmailCallback() which was throwing an NPE.

Retrofit - use response class with inheritance

I'm trying make reuse of single retrofit api call by inherit from a base response class.
However I'm not able to do it.
I will try to make myself clear with example (It's not a concrete scenario. I'm just trying to figure out the main idea):
Having this response objects and api service:
public class UserDetailsResponse
{
private int userId;
}
public class ExtendedUserDetailsResponse extends UserDetailsResponse
{
private int userAdditionalId;
}
interface APIService
{
#GET("/UserDetails/")
Call<UserDetailsResponse> getUserDetails(#Query("id") String userId);
}
Is there a way of using getUserDetails api with ExtendedUserDetailsResponse object?
This one gives me compilation error:
mService.getUserDetails("123").enqueue(new Callback<ExtendedUserDetailsResponse>()
{
#Override
public void onResponse(Call<ExtendedUserDetailsResponse> call, Response<ExtendedUserDetailsResponse> response)
{
}
#Override
public void onFailure(Call<ExtendedUserDetailsResponse> call, Throwable t)
{
}
});
How can I solve this? or at least something similar to this, without using a new api call for the specific derived class?
Thanks!
You get an error because ExtendedUserDetailsResponse is a UserDetailsResponse, however, UserDetailsResponse is not necessarily an ExtendedUserDetailsResponse.
In order to make it generic, sign your method this way
Call< ExtendedUserDetailsResponse > getUserDetails(#Query("id") String userId);
Then ExtendedUserDetailsResponse will have access to userId
Remember to expose userId with getters and setters so that it get parsed.
You are getting compilation error because you are using the wrong callback object:
Just change this line:
Call<UserDetailsResponse> getUserDetails(#Query("id") String userId);
to
Call<ExtendedUserDetailsResponse> getUserDetails(#Query("id") String userId);
Or depending on the response change the object in the callback
Note that ExtendedUserDetailsResponse will have userId so you can use ExtendedUserDetailsResponse even if the server returns object of type UserDetailsResponse or ExtendedUserDetailsResponse.

how to test picasso using unit-test and mockito

I am learning how to unit-testing in android studio. as shown below, I would like to test the two methods shown below in the code section.
can you please help and guide me how to test this method?
code
public RequestCreator requestCreatorFromUrl(String mPicUrl)
{
return Picasso.with(mCtx).load(mPicUrl);
}
public void setImageOnImageView(RequestCreator requestCreator, ImageView mImagView)
{
requestCreator.into(mImagView);
}
My Attempts:
#Test
public void whenRequestCreatorFromUrlTest() throws Exception {
Picasso mockPicasso = mock(Picasso.class);
File mockFile = mock(File.class);
Assert.assertNotNull("returned Request creator is not null",
mockPicasso.load(mockFile));
}
First method you can't test, you'd have to verify the call of a static method which is not supported in Mockito.
You could split the method in
public RequestCreator requestCreator() {
return Picasso.with(mCtx);
}
and
public void load(RequestCreator requestCreator, String picUrl) {
requestCreator.load(picUrl)
}
and test the load(...) method.
Second method:
Mock the requestCreator. Mock the imageView.
Call the method with your mocked objects.
Then verify requestCreator.into(...) was called with the supplied parameter:
Mockito.verify(requestCreator).into(imageView);

Mockito thenReturn not working as expected

I'm having problem with mockito. I'm mocking a class and then using thenReturn() on on of its method. but seems like something is going wrong. here is the code.
TestCode:
public void getCardsTest() {
FeatureFragmentPresenterImpl presenter = new FeatureFragmentPresenterImpl();
GroupFeatureData data = Mockito.mock(GroupFeatureData.class);
FeatureFragmentView view = Mockito.mock(FeatureFragmentView.class);
presenter.init(view, data);
Observable<Response<ResponseBody>> errorObservable = Observable.error(new IOException());
assertNotNull(observable);
Mockito.when(data.getCards(Mockito.anyString(), Mockito.anyString(),
Mockito.anyInt(), Mockito.anyInt())).
thenReturn(errorObservable);
presenter.getAllCards(new Contact(new Name("ssd")), -1);
}
Presenter code :
public void getAllCards(IContact iContact, int lastIndex) {
Observable<Response<ResponseBody>> allCardsResponseObservable = mGroupFeatureData.getCards(path, id, 10, lastIndex);
allCardsResponseObservable
.subscribeOn(Schedulers.io()) -------> Test Failing because NPE here
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new DisposableObserver<Response<ResponseBody>>() {
#Override
public void onNext(#NonNull Response<ResponseBody> response) {
}
#Override
public void onError(#NonNull Throwable e) {
}
#Override
public void onComplete() {
});
}
public void init(FeatureFragmentView featureFragmentView,
GroupFeatureData groupFeatureData) {
this.mGroupFeatureData = groupFeatureData;
this.mFeatureFragmentView = featureFragmentView;
}
Even though i'm mocking response of data.getCards() in Test, In presenter it is throwing NPE whereas it should just operate on mocked Observable that is errorObservable. what is going wrong here?
The NPE tells us that this line:
mGroupFeatureData.getCards(path, id, 10, lastIndex);
... returns null which implies that the actual method call and the method call which you mocked here ...
Mockito.when(data.getCards(Mockito.anyString(), Mockito.anyString(),
Mockito.anyInt(), Mockito.anyInt())).
thenReturn(errorObservable);
... do not match. The code supplied shows this actual call:
Observable<Response<ResponseBody>> allCardsResponseObservable =
mGroupFeatureData.getCards(path, id, 10, lastIndex);
Breaking this call down we can say that:
The third argument 10 is an int so this will match the given argument matcher: Mockito.anyInt()
The fourth argument lastIndex is declared as an int so this will match the given argument matcher: Mockito.anyInt()
The type of the first and second argments is not clear from your code extract since we do not see where path and id are declared but unless they are both of type String then the given argument matchers for these parameters (Mockito.anyString()) will not match and hence the mocked call will return null.
So, it looks to me like one or other of path and id are not actually of type String. It would be useful if you could update your question to show where these types are declared.

Mapping error responses with 200 response code in Retrofit

I'm dealing with an API that I don't control which returns error responses in JSON format, but doesn't return a non-200 response code in that case. Is it still possible to get anything that is an error (as determined by the presence of an "error" property) in the failure callback when using Retrofit? It's safe to assume I can recognise error responses from that API by looking at the message contents.
This is an application-level distinction which means Retrofit doesn't (and shouldn't) care about it.
There's three ways to accomplish what you want, each of which maps to a behavior of Retrofit.
For asynchronous invocation you can use a custom Callback subtype which does the mapping.
public abstract class MyCallback<T extends MyResponse> implements Callback<T> {
#Override public final void success(T data, Response response) {
if (!data.success) {
success(data);
} else {
error(data.error, response);
}
// This is just an example of what a potential common handler could look like.
}
public abstract void success(T data);
public abstract void httpError(Error error, Response response);
}
If you are using the experimental RxJava support you should map the Observable through something which extracts the error.
service.doSomething()
.map(new Func1<SomethingResponse, SomethingResponse>() {
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(..);
Finally, for synchronous you can obviously check at every call site, but you can also wrap the API.
public final class WrappedService implements Service {
private final Service real;
#Override public SomethingResponse doSomething() {
return handleError(real.doSomething());
}
private static <T extends MyResponse> T handleError(T data) {
if (!data.success) {
throw new SomeException(data.error);
}
return data;
}
}
(Hint: You can also use a Proxy to do this for every method automatically!)
The important thing to remember here is that Retrofit's responsibility is to map your HTTP API to a Java API. It is not responsible for applying application-level behavior or constraints to the request or response data. That remains your responsibility.
Looks this is possible in the Converter, from the javadoc:
ConversionException - if conversion was unable to complete. This will trigger a call to
Callback.failure(retrofit.RetrofitError) or throw a RetrofitError. The exception message
should report all necessary information about its cause as the response body will be set to
null.

Categories

Resources