I am trying to access a function call inside a method so I can test the FileReader sonar is highlighting. I need to test the transform() method. I want to high jack the calls inside that method. So, I can pass in a mock file.
Reader gmFile = createFileReader(getGmFile());
Here is my test function which doesnt do what i want.
@Test
public void testFileReader() throws Exception {
File gmFile = mock(File.class);
File smsFile = mock(File.class);
File outFile = mock(File.class);
//execute
SmsTransformationImpl sms = new SmsTransformationImpl(gmFile, smsFile, outFile);
Reader reader = Whitebox.<Reader>invokeMethod(sms, "createFileReader", smsFile);
//Reader gmFile = createFileReader(getGmFile());
}
Here is the whole class.
public class SmsTransformationImpl implements SmsTransformation {
private final File gmFile;
private final File smsFile;
private final File out;
/**
* Constructor to set the files
*
* @param gm gmFile
* @param sms smsFile
* @param out outFile
*/
public SmsTransformationImpl(File gm, File sms, File out) {
if (gm == null || sms == null || out == null) {
throw new IllegalArgumentException("Files have no content");
}
this.gmFile = gm;
this.smsFile = sms;
this.out = out;
}
/**
* This method return the sms file object
*
* @return smsFile
*/
public File getSmsFile() {
return smsFile;
}
/**
* This method returns the gm file object
*
* @return gmFile
*/
public File getGmFile() {
return gmFile;
}
/**
* This method returns the out file object
*
* @return outFile
*/
public File getOutFile() {
return out;
}
/**
* This function uses file reader to read in the files
* ready to write
*/
@Override
public void transform() throws IOException {
try (
Reader gmFile = createFileReader(getGmFile());
Reader smsFile = createFileReader(getSmsFile());
Writer outFile = createFileWriter(getOutFile());
){
transform(gmFile, smsFile, outFile);
}
}
/**
* @param gm gm file
* @param sms sms file
* @param out out file
* @throws IOException
*/
public static void transform(Reader gm, Reader sms, Writer out) throws IOException {
writeStream(out, gm);
writeStream(out, sms);
}
private static void writeStream(Writer writer, Reader reader) throws IOException {
int count = 0;
char[] buffer = new char[1024];
while ((count = reader.read(buffer)) != -1) {
writer.write(buffer, 0, count);
}
}
private Reader createFileReader(File file) throws IOException {
return new FileReader(file);
}
private Writer createFileWriter(File file) throws IOException {
return new FileWriter(file);
}
}
How can use PowerMock to call transform().createFileReader(mock file); to pass in a mock File.
Aucun commentaire:
Enregistrer un commentaire