testcase for jokeloadertask
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
package com.udacity.gradle.builditbigger;
|
||||||
|
|
||||||
|
import android.test.AndroidTestCase;
|
||||||
|
|
||||||
|
import net.headlezz.jokesbackend.myApi.model.Joke;
|
||||||
|
|
||||||
|
public class JokeLoaderTaskTest extends AndroidTestCase implements JokeLoaderTask.JokeLoaderCallback {
|
||||||
|
|
||||||
|
SyncronizeTalker talker;
|
||||||
|
|
||||||
|
public void testJokeLoader() {
|
||||||
|
JokeLoaderTask task = new JokeLoaderTask(this);
|
||||||
|
talker = new SyncronizeTalker();
|
||||||
|
|
||||||
|
task.execute();
|
||||||
|
talker.doWait();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onJokeLoaded(Joke joke) {
|
||||||
|
assertNotNull(joke);
|
||||||
|
assertTrue(joke.getJoke() != null && !joke.getJoke().isEmpty());
|
||||||
|
talker.doNotify();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError() {
|
||||||
|
assertTrue(false);
|
||||||
|
talker.doNotify();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package com.udacity.gradle.builditbigger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper class to wait and release in a testcase
|
||||||
|
* Source by Maxim Shoustin
|
||||||
|
* http://stackoverflow.com/questions/2321829/android-asynctask-testing-with-android-test-framework
|
||||||
|
*/
|
||||||
|
public class SyncronizeTalker {
|
||||||
|
public void doWait(long l){
|
||||||
|
synchronized(this){
|
||||||
|
try {
|
||||||
|
this.wait(l);
|
||||||
|
} catch(InterruptedException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void doNotify() {
|
||||||
|
synchronized(this) {
|
||||||
|
this.notify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void doWait() {
|
||||||
|
synchronized(this){
|
||||||
|
try {
|
||||||
|
this.wait();
|
||||||
|
} catch(InterruptedException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user