Reputation: 3548
So I have a service class.
public class OrganisationService {
public List<Organisation> findAllForeignOrganisations() {
// a few rows of JPQL-code that searches the database
return myCriteria.getResultList();
}
//...Other methods that return Lists with other types of organisations...
}
Then I have a class which I want to test. What I want to test is, when this following class's method getAllowedOrganisations
is given certain parameter (OrganisationType.FOREIGN
in this case), uses the aforementioned method findAllForeignOrganisation() to search for a List of organisations.
public class OrganisationSelectionOptions {
@Inject OrganisationService os;
public List<Organisation> getAllowedOrganisations(Assignment a) {
OrganisationType type = a.getOrganisationType();
return giveOrganisationListWithType(type);
}
private List<Organisation> giveOrganisationListWithType(OrganisationType type) {
List<Organisation> organisations;
if (type == OrganisationType.FOREIGN) {
organisations = os.findAllForeignOrganisations();
}
// ...some other organisations types on if-clauses...
return organisations;
}
}
}
I made a JUnit test to test this. I have to create mocks of these aforementioned classes because they access database and currently I can't access the database in tests and would like to avoid implementing a database structure to tests.
@RunWith(Arquillian.class)
@PrepareForTest(Organisation.class)
public class OrganisationSelectionTest {
@Test
public void testWithForeignType() throws RreflectionUtilException {
// ...code to create a dummy Assignment with FOREIGN type organisation...
mockedOrganisationService = mock(OrganisationService.class);
mockedOrganisationSelectionOptions = mock(OrganisationSelectionOptions.class);
}
}
Now I tried Mockito's verify
to somehow test, if mockedOrganisationSelectionOptions.giveOrganisationListWithType(dummyAssignmentWithForeignTypeOrganisation)
would call os.findAllForeignOrganisations();
to no avail.
I'm a clueless beginner on creating tests. Any help would be vastly appreciated and rewarded, thank you. I'm ready to answer any comments/questions to give more detail on the issue.
Upvotes: 0
Views: 2373
Reputation: 11992
This can be done via multiple ways but I will go over one. First, because you use @Inject to get your instance of OrganisationService, then I will not mess around with mocking the new OrganisationService()
constructor call. Instead we can setup a getter method for the OrganisationService, then mock it.
Add a getter in OrganisationSelectionOptions .
OrganisationService getOrganisationService(){
return os;
}
Edit your giveOrganisationListWithType method to use our new getter.
organisations = getOS().findAllForeignOrganisations();
In your unit test mock an instance of OrganisationService. There are two ways to do this.
method one: add the following in ToimeksiannonYhtiovalintaTest.
@Mock
private OrganisationService mockOS;
method two: instantiate your mockOS in the method doing the testing.
OrganisationService mockOS = mock(OrganisationService.class);
For method one to work you need to include..
@RunWith(MockitoJUnitRunner.class)
in ToimeksiannonYhtiovalintaTest. This is a class level annotation.
Next we need to spy the object being tested.
OrganisationSelectionOptions toTest = spy( new OrganisationSelectionOptions ());
then mock our new getter method.
when(toTest.getOrganisationService()).thenReturn(mockOS);
There you go.
EDIT: added...
Edit your giveOrganisationListWithType method to use our new getter.
organisations = getOS().findAllForeignOrganisations();
Upvotes: 2