Permalink
Browse files

adding hook to the JUnitCore listener

  • Loading branch information...
1 parent 5854159 commit 788fba77925008027dc6d6a1909c83914ab7b86a Sebastian Hermida committed Apr 2, 2011
View
@@ -5,5 +5,6 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="lib" path="vendor/mockito-all-1.8.5.jar"/>
+ <classpathentry kind="lib" path="vendor/org.eclipse.jdt.core_3.6.1.v_A68_R36x.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
View
@@ -7,6 +7,7 @@ Bundle-Activator: com.happyprog.tdgotchi.Activator
Bundle-Vendor: HAPPYPROG
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
- org.junit
+ org.junit,
+ org.eclipse.jdt.junit.core;bundle-version="3.6.1"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
@@ -1,21 +1,33 @@
package com.happyprog.tdgotchi.subscriber;
+import org.eclipse.jdt.junit.JUnitCore;
+import org.eclipse.jdt.junit.TestRunListener;
+import org.eclipse.jdt.junit.model.ITestElement;
+import org.eclipse.jdt.junit.model.ITestElement.Result;
+import org.eclipse.jdt.junit.model.ITestRunSession;
-public class JUnitTestSubscriber implements TestSubscriber {
+public class JUnitTestSubscriber extends TestRunListener implements TestSubscriber {
private TestObserver observer;
@Override
public void subscribe(TestObserver observer) {
this.observer = observer;
+ addAsTestListener();
}
- public void onPassingTest() {
- observer.onPassingTest();
+ void addAsTestListener() {
+ JUnitCore.addTestRunListener(this);
}
- public void onFailingTest() {
- observer.onFailingTest();
+ @Override
+ public void sessionFinished(ITestRunSession session) {
+ Result testResult = session.getTestResult(true);
+ if (testResult.equals(ITestElement.Result.OK)) {
+ observer.onPassingTest();
+ } else {
+ observer.onFailingTest();
+ }
}
}
@@ -2,37 +2,44 @@
import static org.mockito.Mockito.*;
+import org.eclipse.jdt.junit.model.ITestElement;
+import org.eclipse.jdt.junit.model.ITestRunSession;
import org.junit.Before;
import org.junit.Test;
-import com.happyprog.tdgotchi.subscriber.JUnitTestSubscriber;
-import com.happyprog.tdgotchi.subscriber.TestObserver;
-
-
public class JUnitTestSubscriberTest {
private TestObserver observer;
private JUnitTestSubscriber subscriber;
+ private ITestRunSession session;
@Before
public void before() {
+ session = mock(ITestRunSession.class);
observer = mock(TestObserver.class);
- subscriber = new JUnitTestSubscriber();
+
+ subscriber = spy(new JUnitTestSubscriber());
+ doNothing().when(subscriber).addAsTestListener();
subscriber.subscribe(observer);
}
@Test
public void onPassingTest_updateObserver() throws Exception {
- subscriber.onPassingTest();
+ when(session.getTestResult(true)).thenReturn(ITestElement.Result.OK);
+
+ subscriber.sessionFinished(session);
verify(observer).onPassingTest();
}
@Test
public void onFailingTest_updateObserver() throws Exception {
- subscriber.onFailingTest();
+ when(session.getTestResult(true)).thenReturn(ITestElement.Result.FAILURE);
+
+ subscriber.sessionFinished(session);
verify(observer).onFailingTest();
}
+
}
Binary file not shown.

0 comments on commit 788fba7

Please sign in to comment.