Several options here...
1) Do nothing. Setters and getters are trivial and don't require unit tests.
2) Add a getter function. It matches the setter and is better than a public member variable, but it adds to the public interface for the sole purpose of testing.
3) Make your test class a friend of the class being tested.
Code:
#include <cassert>
class Boo {
int i;
friend class BooTest;
public:
Boo(int j) : i(j) {}
void set(int p) { i = p; }
};
class BooTest {
public:
void testSet()
{
Boo b(7);
b.set(0);
assert(b.i == 0);
}
};
int main()
{
BooTest bt;
bt.testSet();
return 0;
}
4) Write "self-testing" code, as Cline, Lomow, and Girou suggest in the C++ FAQs book:
Code:
#include <cassert>
class Boo {
int i;
public:
Boo(int j) : i(j) {}
void set(int p)
{
i = p;
#ifdef UNITTEST
assert(i == p);
#endif
}
};
int main()
{
Boo b(4);
b.set(1);
return 0;
}
I'd opt for 1) for simple setters (and would try to refactor the setters out of the code).