If there are objects being instantiated on the action due to lack of contructor injection how can you mock them to test?
You can refactor your code to encapsulate all code that should be mocked away into a
seperate protected methods and then use partial-mocks to replace that logic.
Example (in java)
Original
protected void moveFiles(File[] destFiles, File[] sourceFiles) {
int pos = 0;
while (pos < fileCount) {
File sourceFile = sourceFiles[pos];
File destFile = destFiles[pos];
// do some processing. i.e. rename destFile if it already exists.
// do the copy. the test should not execute this
sourceFile.renameTo(destFile)
pos++;
}
}
refactored:
protected void moveFiles(File[] destFiles, File[] sourceFiles) {
int pos = 0;
while (pos < fileCount) {
File sourceFile = sourceFiles[pos];
File destFile = destFiles[pos];
// do some processing. i.e. rename destFile if it already exists.
osFileMove(destFile, sourceFile);
pos++;
}
}
/** can be replaced by mock/stub in unittests */
protected boolean osFileMove(File destFile, File sourceFile) {
return sourceFile.renameTo(destFile);
}
in the test you can replace osFileMove
with a fake (example uses java org.mockito.Mockito)
import static org.mockito.Mockito.*;
FileCommands sut;
@Before
public void setup() {
sut = spy(new FileCommands());
doReturn(true).when(sut).osCreateDirIfNeccessary(any(File.class));
doReturn(true).when(sut).osFileCopy(any(File.class), any(File.class));
doReturn(true).when(sut).osFileMove(any(File.class), any(File.class));
doReturn(true).when(sut).osDeleteFile(any(File.class));
}
If you later decide to use ioc you can move the os-spcific file operations osxXXXX
into a seperate class plus an interface