Skip to content
Snippets Groups Projects
Commit 6090088c authored by Julian Dehne's avatar Julian Dehne
Browse files

bug: fixed problem in mocking

parent 467f590d
No related branches found
No related tags found
No related merge requests found
Showing
with 189 additions and 24 deletions
...@@ -4,6 +4,8 @@ import org.glassfish.hk2.utilities.binding.AbstractBinder; ...@@ -4,6 +4,8 @@ import org.glassfish.hk2.utilities.binding.AbstractBinder;
import unipotsdam.gf.core.management.Management; import unipotsdam.gf.core.management.Management;
import unipotsdam.gf.core.management.ManagementImpl; import unipotsdam.gf.core.management.ManagementImpl;
import unipotsdam.gf.core.states.PhasesImpl; import unipotsdam.gf.core.states.PhasesImpl;
import unipotsdam.gf.core.testsandbox.TestList;
import unipotsdam.gf.core.testsandbox.TestListInterface;
import unipotsdam.gf.interfaces.*; import unipotsdam.gf.interfaces.*;
import unipotsdam.gf.modules.assessment.controller.service.PeerAssessment; import unipotsdam.gf.modules.assessment.controller.service.PeerAssessment;
import unipotsdam.gf.modules.assessment.controller.service.PeerAssessmentDummy; import unipotsdam.gf.modules.assessment.controller.service.PeerAssessmentDummy;
...@@ -31,5 +33,6 @@ public class GFApplicationBinder extends AbstractBinder { ...@@ -31,5 +33,6 @@ public class GFApplicationBinder extends AbstractBinder {
bind(PhasesImpl.class).to(IPhases.class); bind(PhasesImpl.class).to(IPhases.class);
bind(ManagementImpl.class).to(Management.class); bind(ManagementImpl.class).to(Management.class);
bind(DummyResearchReportManagement.class).to(ResearchReportManagement.class); bind(DummyResearchReportManagement.class).to(ResearchReportManagement.class);
bind(TestList.class).to(TestListInterface.class);
} }
} }
...@@ -118,9 +118,7 @@ public class PhasesImpl implements IPhases { ...@@ -118,9 +118,7 @@ public class PhasesImpl implements IPhases {
} }
public void setFeedback(Feedback feedback) {
this.feedback = feedback;
}
} }
package unipotsdam.gf.core.testsandbox;
import java.util.ArrayList;
public class TestList extends ArrayList implements TestListInterface {
}
package unipotsdam.gf.core.testsandbox;
public interface TestListInterface extends java.util.List {
}
...@@ -10,4 +10,10 @@ public interface IPhases { ...@@ -10,4 +10,10 @@ public interface IPhases {
* @param project * @param project
*/ */
public void endPhase(ProjectPhase projectPhase, Project project); public void endPhase(ProjectPhase projectPhase, Project project);
/**
* the dependency to feedback should be settable externally for test reasons
* @param feedback
*/
void setFeedback(Feedback feedback);
} }
...@@ -17,6 +17,7 @@ public class DummyFeedback implements Feedback { ...@@ -17,6 +17,7 @@ public class DummyFeedback implements Feedback {
*/ */
PodamFactory factory = new PodamFactoryImpl(); PodamFactory factory = new PodamFactoryImpl();
private static Boolean missingTaskAssigned = false;
@Override @Override
public Peer2PeerFeedback createPeer2PeerFeedbackmask( public Peer2PeerFeedback createPeer2PeerFeedbackmask(
...@@ -43,12 +44,13 @@ public class DummyFeedback implements Feedback { ...@@ -43,12 +44,13 @@ public class DummyFeedback implements Feedback {
public Boolean checkFeedbackConstraints(Project project) { public Boolean checkFeedbackConstraints(Project project) {
// TODO implement cornstaints // TODO implement cornstaints
System.out.println("Checking fake constraints"); System.out.println("Checking fake constraints");
return true; return missingTaskAssigned;
} }
@Override @Override
public void assigningMissingFeedbackTasks(Project project) { public void assigningMissingFeedbackTasks(Project project) {
System.out.println("assigning fake tasks"); System.out.println("assigning fake tasks");
missingTaskAssigned = true;
} }
@Override @Override
......
package unipotsdam.gf.modules.researchreport;
public class DummyResearchReportCounter {
public static Boolean feedbackTasksNotAssigned = true;
}
package unipotsdam.gf.modules.researchreport; package unipotsdam.gf.modules.researchreport;
import org.mockito.Mock;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import uk.co.jemos.podam.api.PodamFactory; import uk.co.jemos.podam.api.PodamFactory;
import uk.co.jemos.podam.api.PodamFactoryImpl; import uk.co.jemos.podam.api.PodamFactoryImpl;
import unipotsdam.gf.core.management.project.Project; import unipotsdam.gf.core.management.project.Project;
...@@ -14,15 +12,27 @@ import java.io.File; ...@@ -14,15 +12,27 @@ import java.io.File;
public class DummyResearchReportManagement implements ResearchReportManagement { public class DummyResearchReportManagement implements ResearchReportManagement {
/** /**
* Utility to creaty dummy data for students * Utility to creaty dummy data for students
*/ */
PodamFactory factory = new PodamFactoryImpl(); PodamFactory factory = new PodamFactoryImpl();
@Inject
Feedback feedback;
@Override @Override
public String createResearchReport( public String createResearchReport(
ResearchReport researchReport, Project project, User student) { ResearchReport researchReport, Project project, User student) {
// real implementation should check if all the constraints are ok before starting with feedbacks
// this assumes uploading and giving feedback is in the same phase (no teacher decision to go from
// uploading dossiers to feedback
if (DummyResearchReportCounter.feedbackTasksNotAssigned) {
DummyResearchReportCounter.feedbackTasksNotAssigned = false;
feedback.assignFeedbackTasks();
}
return factory.manufacturePojo(ResearchReport.class).getId(); return factory.manufacturePojo(ResearchReport.class).getId();
} }
...@@ -40,4 +50,15 @@ public class DummyResearchReportManagement implements ResearchReportManagement { ...@@ -40,4 +50,15 @@ public class DummyResearchReportManagement implements ResearchReportManagement {
public File getResearchReport(ResearchReport researchReport) { public File getResearchReport(ResearchReport researchReport) {
return null; return null;
} }
@Override
public void createFinalResearchReport(
ResearchReport researchReport, Project project, User student) {
}
@Override
public void setFeedback(Feedback feedback) {
this.feedback = feedback;
}
} }
...@@ -2,6 +2,7 @@ package unipotsdam.gf.modules.researchreport; ...@@ -2,6 +2,7 @@ package unipotsdam.gf.modules.researchreport;
import unipotsdam.gf.core.management.project.Project; import unipotsdam.gf.core.management.project.Project;
import unipotsdam.gf.core.management.user.User; import unipotsdam.gf.core.management.user.User;
import unipotsdam.gf.interfaces.Feedback;
import java.io.File; import java.io.File;
...@@ -48,5 +49,19 @@ public interface ResearchReportManagement { ...@@ -48,5 +49,19 @@ public interface ResearchReportManagement {
File getResearchReport(ResearchReport researchReport); File getResearchReport(ResearchReport researchReport);
/**
* This represents a second version of the research report where the feedback is incorporated
* There is only this second version. Otherwise we could cycle the dossier upload and feedback (would be to
* complicated)
* @param researchReport
* @param project
* @param student
*/
void createFinalResearchReport(ResearchReport researchReport, Project project, User student);
/**
* the dependency to feedback should be settable externally for test reasons
* @param feedback
*/
void setFeedback(Feedback feedback);
} }
...@@ -7,6 +7,9 @@ import org.junit.Rule; ...@@ -7,6 +7,9 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.MockitoRule; import org.mockito.junit.MockitoRule;
...@@ -35,9 +38,6 @@ import static org.mockito.Mockito.verify; ...@@ -35,9 +38,6 @@ import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class ActivityFlowTest { public class ActivityFlowTest {
Boolean mocked = true;
/** /**
* Utility to creaty dummy data for students * Utility to creaty dummy data for students
*/ */
...@@ -49,12 +49,11 @@ public class ActivityFlowTest { ...@@ -49,12 +49,11 @@ public class ActivityFlowTest {
@Inject @Inject
ResearchReportManagement researchReportManagement; ResearchReportManagement researchReportManagement;
@Mock
Feedback mockfeedback;
@Inject @Inject
Feedback feedback; Feedback feedback;
@Inject @Inject
IPhases phases; IPhases phases;
...@@ -69,9 +68,14 @@ public class ActivityFlowTest { ...@@ -69,9 +68,14 @@ public class ActivityFlowTest {
public void setUp() { public void setUp() {
final ServiceLocator locator = ServiceLocatorUtilities.bind(new GFApplicationBinder()); final ServiceLocator locator = ServiceLocatorUtilities.bind(new GFApplicationBinder());
locator.inject(this); locator.inject(this);
}
feedback = Mockito.spy(feedback);
researchReportManagement = Mockito.spy(researchReportManagement);
phases = Mockito.spy(phases);
researchReportManagement.setFeedback(feedback);
phases.setFeedback(feedback);
}
@Test @Test
public void activityPlayer() { public void activityPlayer() {
...@@ -126,17 +130,16 @@ public class ActivityFlowTest { ...@@ -126,17 +130,16 @@ public class ActivityFlowTest {
public void uploadDossiers() { public void uploadDossiers() {
for (User student : students) { for (User student : students) {
// persist dossiers // persist dossiers
ResearchReport researchReport = factory.manufacturePojo(ResearchReport.class); ResearchReport researchReport = factory.manufacturePojo(ResearchReport.class);
researchReportManagement.createResearchReport(researchReport, project, student); researchReportManagement.createResearchReport(researchReport, project, student);
} }
if (mocked) {
mockfeedback.assignFeedbackTasks();
}
// assert that after the last report has been submitted, the feedback tasks were assigned automatically // assert that after the last report has been submitted, the feedback tasks were assigned automatically
verify(mockfeedback, times(1)).assignFeedbackTasks(); verify(feedback).assignFeedbackTasks();
// students give feedback // students give feedback
for (User student : students) { for (User student : students) {
...@@ -158,20 +161,20 @@ public class ActivityFlowTest { ...@@ -158,20 +161,20 @@ public class ActivityFlowTest {
Iterator<User> students2Iterator = students2.iterator(); Iterator<User> students2Iterator = students2.iterator();
while (students2Iterator.hasNext()) { while (students2Iterator.hasNext()) {
User student = students2Iterator.next(); User student = students2Iterator.next();
// persist dossiers (versioning) // persist final dossiers -- assuming this function is intended
// if only one time upload is intended and feedback is not incorporated into a final dossier
// you should change this test to reflect only one time upload
// i.e. removing one student above to reflect no compliance
ResearchReport researchReport = factory.manufacturePojo(ResearchReport.class); ResearchReport researchReport = factory.manufacturePojo(ResearchReport.class);
researchReportManagement.createResearchReport(researchReport, project, student); researchReportManagement.createFinalResearchReport(researchReport, project, student);
} }
// docent finishes phase // docent finishes phase
phases.endPhase(ProjectPhase.DossierFeedback, project); phases.endPhase(ProjectPhase.DossierFeedback, project);
if (mocked) {
mockfeedback.assigningMissingFeedbackTasks(project);
}
// student misses mockfeedback -> reassignment // student misses mockfeedback -> reassignment
// assert that while reports are still missing mockfeedback tasks are reassigned // assert that while reports are still missing mockfeedback tasks are reassigned
verify(mockfeedback, times(1)).assigningMissingFeedbackTasks(project); verify(feedback).assigningMissingFeedbackTasks(project);
// assert that everybody has given and received mockfeedback // assert that everybody has given and received mockfeedback
assertTrue(feedback.checkFeedbackConstraints(project)); assertTrue(feedback.checkFeedbackConstraints(project));
...@@ -192,4 +195,5 @@ public class ActivityFlowTest { ...@@ -192,4 +195,5 @@ public class ActivityFlowTest {
// //
} }
} }
package unipotsdam.gf.testsandbox;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import unipotsdam.gf.config.GFApplicationBinder;
import unipotsdam.gf.core.testsandbox.TestListInterface;
import javax.inject.Inject;
import java.util.ArrayList;
import static junit.framework.Assert.assertEquals;
@RunWith(MockitoJUnitRunner.class)
public class TestTestTest {
@Spy
java.util.List<String> spiedList = new ArrayList<String>();
@Inject
TestListInterface spiedList2;
@Inject
@Spy
TestListInterface spiedList3;
@Before
public void setUp() {
final ServiceLocator locator = ServiceLocatorUtilities.bind(new GFApplicationBinder());
locator.inject(this);
}
// GEHT
@Test
public void howSpiesWork() {
java.util.List<String> spiedList = Mockito.spy(new ArrayList<String>());
Mockito.spy(spiedList);
spiedList.add("one");
spiedList.add("two");
Mockito.verify(spiedList).add("one");
Mockito.verify(spiedList).add("two");
assertEquals(2, spiedList.size());
}
// GEHT
@Test
public void howSpiesWorkWithAnnotation() {
spiedList.add("one");
spiedList.add("two");
Mockito.verify(spiedList).add("one");
Mockito.verify(spiedList).add("two");
assertEquals(2, spiedList.size());
}
// GEHT
@Test
public void howSpiesWorkWithInjection() {
java.util.List<String> spiedList = Mockito.spy(spiedList2);
spiedList.add("one");
spiedList.add("two");
Mockito.verify(spiedList).add("one");
Mockito.verify(spiedList).add("two");
assertEquals(2, spiedList.size());
}
// GEHT NICHT!
@Test
public void howSpiesWorkWithInjectionAndAnnotation() {
spiedList3.add("one");
spiedList3.add("two");
Mockito.verify(spiedList3).add("one");
Mockito.verify(spiedList3).add("two");
assertEquals(2, spiedList3.size());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment