Skip to content

Commit 5cf2c7e

Browse files
committed
failing unit test fest exceptions in background thread
1 parent 94716f8 commit 5cf2c7e

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

jobqueue/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.path.android.jobqueue"
44
android:versionCode="2"
5-
android:versionName="1.1.1">
5+
android:versionName="1.1.2-SNAPSHOT">
66
<application android:label="">
77
</application>
88
</manifest>

jobqueue/test/com/path/android/jobqueue/test/jobmanager/InjectorTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
import com.path.android.jobqueue.Params;
77
import com.path.android.jobqueue.config.Configuration;
88
import com.path.android.jobqueue.di.DependencyInjector;
9+
import com.path.android.jobqueue.log.CustomLogger;
910
import com.path.android.jobqueue.test.jobs.DummyJob;
1011
import static org.hamcrest.CoreMatchers.*;
1112
import org.hamcrest.*;
1213
import org.junit.Test;
1314
import org.junit.runner.RunWith;
1415
import org.robolectric.*;
1516

17+
import java.util.concurrent.CountDownLatch;
18+
import java.util.concurrent.TimeUnit;
1619
import java.util.concurrent.atomic.AtomicInteger;
1720

1821
@RunWith(RobolectricTestRunner.class)
@@ -42,6 +45,55 @@ public void inject(BaseJob job) {
4245
holder = getNextJobMethod(jobManager).invoke();
4346
MatcherAssert.assertThat("injection should be called for persistent job", holder.getBaseJob(), equalTo(injectedJobReference.getObject()));
4447
MatcherAssert.assertThat("injection should be called two times for persistent job", injectionCallCount.get(), equalTo(3));
48+
}
49+
50+
@Test
51+
public void testInjectorCrash() throws Exception {
52+
final String EXCEPTION_MESSAGE = "could not inject for whatever reason :)";
53+
DependencyInjector dummyDependencyInjector = new DependencyInjector() {
54+
@Override
55+
public void inject(BaseJob baseJob) {
56+
throw new RuntimeException(EXCEPTION_MESSAGE);
57+
}
58+
};
59+
60+
final ObjectReference objectReference = new ObjectReference();
61+
final CountDownLatch exceptionLatch = new CountDownLatch(1);
62+
CustomLogger customLogger = new CustomLogger() {
63+
@Override
64+
public boolean isDebugEnabled() {
65+
return false;
66+
}
67+
68+
@Override
69+
public void d(String s, Object... objects) {
70+
71+
}
72+
73+
@Override
74+
public void e(Throwable throwable, String s, Object... objects) {
75+
objectReference.setObject(throwable);
76+
exceptionLatch.countDown();
77+
}
78+
79+
@Override
80+
public void e(String s, Object... objects) {
81+
//
82+
}
83+
};
84+
JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).injector(dummyDependencyInjector));
85+
Throwable addException = null;
86+
try {
87+
jobManager.addJob(new DummyJob(new Params(0)));
88+
} catch (Throwable t) {
89+
addException = t;
90+
}
91+
MatcherAssert.assertThat("addJob should throw exception if dependency injector throws exception", addException, notNullValue());
92+
jobManager.addJobInBackground(new DummyJob(new Params(0)));
93+
exceptionLatch.await(2, TimeUnit.SECONDS);
94+
MatcherAssert.assertThat("there should be a received exception", objectReference.getObject(), notNullValue());
95+
MatcherAssert.assertThat("logged exception should be a runtime exception", objectReference.getObject(), instanceOf(RuntimeException.class));
96+
MatcherAssert.assertThat("logged exception should have expected message", ((Throwable)objectReference.getObject()).getMessage(), is(EXCEPTION_MESSAGE));
4597

4698
}
4799
}

0 commit comments

Comments
 (0)