Issue
I am testing an anroid app using Mockito
. I want to test an exception case, but the method Mockito.doThrow
hangs and the test never gets executed (it neither fails nor succeeds).
Here's the code. PackageDownloader
represents an AsyncTask
. Its method get
gets called inside of the downloadPackage
method. But the execution never comes to that point because it hangs exactly on doThrow
line.
PackageDownloader packageDownloader = new PackageDownloader();
PackageDownloader mockPackageDownloader = Mockito.spy(packageDownloader);
HttpURLConnection connectionMock = Mockito.mock(HttpURLConnection.class);
Mockito.doReturn(connectionMock)
.when(mockPackageDownloader)
.createConnection(Mockito.any(URL.class));
Mockito.doThrow(new InterruptedException()).when(mockPackageDownloader).get();
downloadPackage();
Solution
So it turned out I was trying to stub a final method (which is, if you look at the definition, AsyncTask.get()
).
With Mockito, it's impossible to mock final methods.
The problem is I wasn't able to find out why the code hangs, and this is certainly a Mockito fault.
I found out the issue by placing doThrow
after the downloadPackage
method call.
Answered By - annaoomph
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.