It is easy enough to create unit tests for conditional blocks which follow the expected path, but it can sometimes be difficult to contrive data for sources/objects I do not directly control, (databases I do not want to modify or access, environmental variables, etc.) without modifying the source code to add debugging logic (using only unit tests as functions. How would one construct unit tests to test the following specified control blocks?
function(int x)
{
if(x > 10)
{
if(system.day() == "Monday")
print "Monday"
else
// TEST THIS SPACE (BUT ON A MONDAY :)
print "Not Monday"
}
else
{
....
}
}
In the previous function, I can write a function and pass whatever value I like in for x, but how can I test the nested conditional which relies on a a System.date() call that I cannot (easily) modify?
Another example using databases which I do not control and cannot access:
function(int x)
{
try
{
if(x > 10)
{
query_result = database.query()
if(query_results != NULL)
{
print "QUERY NOT NULL"
}
else
{
// TEST THIS SPACE
print "QUERY IS NULL"
}
}
}
}
Obviously, if I was passing in the query, I could control it, but in this case I cannot. These are simple and contrived examples, please expand on these particular cases and any related scenarios which I may not have considered.