I notice that in order to unit test each unit in my OO code, I need to set the access modifiers to public, even on methods that ought to be protected, or probably private. Is this OK practice?
public class EnforceBusinessRules
{
BusinessState m_state;
public EnforceBusinessRules()
{
m_state = START;
}
public bool isInputcurrentlyFormatted(string input)
{
//code goes here to ensure the input passes formatting test
//modify m_state appropriately
}
public bool InputContainsValidStartAndEndTokens(string input)
{
//code goes here to ensure that the start and end tokens of the input are of the type available in the system
//modify m_state appropriately
}
public bool StartEndCommandisValidAccordingtoCurrentSystemSettings(string input)
{
//code goes here to check the start and End codes match the current start and end codes for the day
//modify m_state appropriately
}
// and so on
}
Unit testing is “Black box” testing. You should only test the externally visible elements. If you test all the internal workings then you can’t refactor your code correctly without modifying all your unit tests.