Nullifying after a try for a finally test

33 views
Skip to first unread message

Amol Parnaik

unread,
Feb 7, 2012, 5:22:14 AM2/7/12
to PowerMock
Powermock is great! However, I'm stuck on the following. Take the
following class

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;


public class FileReader{

private final File input;
public FileReader(final String filename) throws
FileNotFoundException {
input = new File(filename);
}

public Board read() throws IOException{
String line;
boolean didTry = false;
Scanner scanner = null;

try {
scanner = new Scanner(new FileInputStream(input), "UTF-8");

while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (!lineIsOK(line)) {
throw new IOException("Bad Line");
}
}
didTry = true;

}finally {
if(scanner != null){
System.out.println("Scanner is fine didTry " + didTry);
scanner.close();
}else{
System.out.println("Scanner is null didTry " + didTry);
}
}
}
}


Can I test the line 'Scanner is null didTry true' with powermockito?

Thanks and regards

Amol

Johan Haleby

unread,
Feb 7, 2012, 6:10:56 AM2/7/12
to powe...@googlegroups.com
Hi, 

Personally I would rather test this with an integration test and write a file to disk during test initialization (using the TemporaryFolder rule in JUnit) and use that file in your test to verify the behavior/state of the method. If you don't want to do this you probably need to use this approach to mock the Scanner class.

Regards,
/Johan


Amol

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.


Brice Dutheil

unread,
Feb 7, 2012, 6:16:17 AM2/7/12
to powe...@googlegroups.com
Hi,

I think you need to make the constructor of FileInputStream throw a FileNotFoundException, that should be fairly easy if you pass a 'input' that do not match any file.

If you actually want to test this statement 'System.out.println("Scanner is null didTry " + didTry);' then I would recommend you these points :
 - Don't use "System.out.println" directly, many if not all quality tool have rules on this matter activated by default.
 - If you actually want to test that line, I would rather have a dependency called Reporter or something, with a method name that explains what you want to report. This reporter could be injected in your code. This could mean you can verify that report.reportScannerIsNull() is called.

But depending on what you want this might not apply. If you want to mock classes in rt.jar, I'm not sure it's doable in the Unit Test only, you might need to actually add the powermock agent; Johan can confirm that.


Cheers,

-- Brice



Amol

Johan Haleby

unread,
Feb 7, 2012, 6:37:26 AM2/7/12
to powe...@googlegroups.com
Brice is right that PowerMock can't mock classes in rt.jar but there's a work-around that's described here that doesn't require any changes to classpath or build environment. If anyone is interested in how it actually works it's described in this blog. 

Regards,
/Johan

Johan Haleby

unread,
Feb 7, 2012, 6:41:37 AM2/7/12
to powe...@googlegroups.com
The first link should be in the previous message should be http://code.google.com/p/powermock/wiki/MockSystem. Sorry.

/Johan

Brice Dutheil

unread,
Feb 7, 2012, 6:46:35 AM2/7/12
to powe...@googlegroups.com
Oh yeah, wrappers :)

Also as a reminder, we usually say 'Don't mock types you don't own'. Of course that depends on the context, but that tagline can save you years later. That's why I would prefer the alternative that Johan proposed or mine.


Cheers,

-- Brice

Amol Parnaik

unread,
Feb 7, 2012, 9:49:22 PM2/7/12
to PowerMock
Thanks, this gave me a few ideas to fix the problem.
Reply all
Reply to author
Forward
0 new messages