KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have a data source from which I can request a list of people that live in a (any) country, and a method which retrieves the people from that data source and sorts them by their name alphabetically. How should I write my unit test to make sure that the sorting part of my method works properly? This is what my SUT looks like: class PeopleStuff { public IData data; public List<Person> getSortedPeopleForCountry(String countryName) { List<Person> people = data.getPeopleForCountry(countryName); Comparator nameComparator = new PersonNameComparator(); Collections.sort(people, nameComparator); return people; } } And this is what my unit test looks like: @Test public void testGetPeopleSortsByPeopleName() { String COUNTRY = "Whatistan"; // set up test (the 3 lines below are actually in a @Before setup method) PeopleStuff peopleStuff = new PeopleStuff(); IData mockData = createNiceMock(IData.class); peopleStuff.data = mockData; // set up data List<PersonName> mockPeopleList = new ArrayList<PersonName>(); mockPeopleList.add(new Person(COUNTRY, "A")); mockPeopleList.add(new Person(COUNTRY, "D")); mockPeopleList.add(new Person(COUNTRY, "B")); mockPeopleList.add(new Person(COUNTRY, "C")); when(mockData.getPeopleForCountry(COUNTRY)).thenReturn(mockPeopleList); // exercise List<String> result = peopleStuff.getSortedPeopleForCountry(COUNTRY); // assert assertEquals("A", result.get(0).name); assertEquals("B", result.get(1).name); assertEquals("C", result.get(2).name); assertEquals("D", result.get(3).name); } What I need to know is if the way I am stubbing the data, running the test and making the assertions is correct, or if there are better ways of doing this. My application has a lot of methods to test and a lot of custom sorting algorithm
Tags (comma-separated)
Save Edits
Cancel