I have a class ListContainer.
The class a container for a list and allow 3 operations:
- add
- remove
- getNotValid (return list of not valid elements).
The list is private in the class, so i cannot check that add really add element.
How do you think I should test the add functionality?
for my opinion I should check it by the getNotValid.
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
Container::Container() {
}
void Container::add(Element element) {
pthread_mutex_lock(&mutex);
list.push_back(element);
pthread_mutex_unlock(&mutex);
}
void Container::remove(unsigned int elementId) {
pthread_mutex_lock(&mutex);
list<Element>::iterator it;
for (it = list.begin(); it != list.end(); ++it) {
if (element->id == elementId) { //TODO is it the real check
list.erase(it);
}
}
pthread_mutex_unlock(&mutex);
}
list<Element> Container::getNotValid() {
list<Element> result;
pthread_mutex_lock(&mutex);
list<Element>::iterator it;
for (it = list.begin(); it != list.end(); ++it) {
if (element->isNotValid()) {
result.push_back(*it);
}
}
pthread_mutex_unlock(&mutex);
return result;
}
Classes are typically tested through either their public interface or (using an integration test) by testing the effect a certain operation will have on some application state.
If the private list does nothing more than allow the user of the API to retrieve invalid items, then I would say you only need to test the list using the
add,removeandgetNotValidmethods.If valid items are used in some way (like being written to a file system or database, for instance) then you should test the class through an integration test and make sure that the desired effect on the application state is indeed taking place.