Junit mocking files using jmockit - java

I am trying to test my java code using jmockit for the first time and I am really confused. I have a method that reads a file and returns the line of strings that reads from the file as a list.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Reader {
public static final int LIMIT = -1;
public static final int EMPTY_FILE = 0;
private String delimiter = ",";
public Reader() {}
public List<List<String>> readFile(String fileName, String delimiter) throws IOException {
List<List<String>> rawData = new ArrayList<>();
File input = new File(fileName);
if (!delimiter.isEmpty())
this.delimiter = delimiter;
if (input.length() == EMPTY_FILE) {
throw new IOException("File is empty. Check file and try again.");
}
BufferedReader reader = new BufferedReader(new FileReader(input));
String line;
while ((line = reader.readLine()) != null) {
List<String> lineData = Arrays.asList(line.split(this.delimiter, LIMIT));
rawData.add(lineData);
}
return rawData;
}
}
I am trying to test this code using mocked readers and bufferedReader but without any luck. Obviously I am doing something wrong but I can't figure out how to do it properly.
What I want is to create a mocked file that will be read and test it like its empty or non empty.
What I have tried so far :
class ReaderTest {
static final String FILENAME = "input.txt";
#Injectable
File mockedFile;
#Mocked
BufferedReader mockedBufferedReader;
#Mocked
FileReader mockedFileReader;
#Test
void readNonEmptyInputFileShouldDoNothing() throws FileNotFoundException {
new Expectations(File.class) {{
new File(anyString);
result = mockedFile;
}};
new Expectations(BufferedReader.class) {{
new FileReader(anyString);
result = mockedFileReader;
new BufferedReader(mockedFileReader);
result = mockedBufferedReader;
}};
Reader reader = new Reader();
Assertions.assertDoesNotThrow(() ->
reader.readFile(FILENAME, FieldsConstants.DELIMITER));
}
}
This test gives me an IllegalArgumentException error:
Invalid Class argument for partial mocking (use a MockUp instead): class java.io.File

I managed to solve my issue using PowerMock and EasyMock API. The only issue i faced was that in the beginning i was using Junit5 and PowerMock wasn't working as intended. I switched to Junit4 and everything worked out just fine. Just some sample code if anyone is interested:
#RunWith(PowerMockRunner.class)
#PrepareForTest({Reader.class})
#PowerMockIgnore({"javax.management.*", "javax.script.*"})
public class ReaderTest {
public static final String TEST_LINE = "2,Ned,Flanders,SELLER,,,LINE_A,Springfield,12.8,TRUE";
public static final String FILENAME = "file_name";
public static final int LIMIT = -1;
private static List<List<String>> tmpList;
#BeforeClass
public static void beforeAll() {
List<String> tmpLine = Arrays.asList(TEST_LINE.split(FieldsConstants.DELIMITER, LIMIT));
tmpList = new ArrayList<>();
tmpList.add(tmpLine);
}
#Test
public void readNonEmptyInputFileShouldDoNothing() throws Exception {
File mockFile = createMockAndExpectNew(File.class, FILENAME);
FileReader mockFileReader = createMockAndExpectNew(FileReader.class, mockFile);
BufferedReader mockBufferedReader = createMockAndExpectNew(BufferedReader.class, mockFileReader);
Reader reader = new Reader();
expect(mockFile.length()).andReturn((long) 100);
expect(mockBufferedReader.readLine()).andReturn(TEST_LINE);
expect(mockBufferedReader.readLine()).andReturn(null);
replayAll();
assertEquals(reader.readFile(FILENAME, ""), tmpList);
verifyAll();
}
}

Related

FileWriter / BufferedReader Java word finder

I have this code set up and I am trying to write a program that looks through a file and finds a specific hidden secret word then replaces the word with "found!" then re-prints the text file in the console. I know how to use reader and writer but I am unsure how i can use them in unison to do this. Code is as follows:
Reader Class:
package Main;
import java.io.*;
public class Read {
private static String line;
FileReader in;
File file;
public Read() {
line = "";
}
public void readFile() throws IOException {
file = new File("C:examplePathName\\ReadWriteExp.txt");
in = new FileReader(file);
BufferedReader br = new BufferedReader(in);
while((line = br.readLine()) != null) {
System.out.println(line);
}
in.close();
}
public String getLine() {
return line;
}
public File getFile() {
return file;
}
}
Writer(change) class:
package Main;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class Change {
public static void main(String[] args) throws IOException{
Read r = new Read();
String line = r.getLine();
FileWriter fw = new FileWriter(r.getFile());
while(line != null) {
if(line.equals("example")) {
fw.write("found!");
}
System.out.println(line);
}
}
}
Am i on the right path or should i combine both of these into one class. Also is this the proper way of writing to a specific line in a text file?
If the file is a reasonable size, you can read it into memory, change what you need and write it back out again:
public static void replaceOccurrences(String match, String replacement, Path path) throws IOException {
Files.write(path, Files.lines(path).map(l -> {
if(l.contains(match)) {
return l.replace(match, replacement);
} else {
return l;
}
}).collect(Collectors.toList()));
}
Alternatively, if you know that the search term occurs only once and you just need to find the position of the occurrence, use the following:
try(BufferedReader reader = Files.newBufferedReader(path)) {
int lineIndex = 0;
String line;
while(!(line = reader.readLine()).contains(match)) {
lineIndex++;
}
System.out.println(lineIndex); // line which contains match, 0-indexed
System.out.println(line.indexOf(match)); // starting position of match in line, 0-indexed
}
If all you have to do is print the converted text to system out (rather than writing it out to a file), the second class isn't really needed. You can accomplish what you need in the readFile() method of the Read class:
public void readFile() throws IOException {
file = new File("C:examplePathName\\ReadWriteExp.txt");
in = new FileReader(file);
BufferedReader br = new BufferedReader(in);
while((line = br.readLine()) != null) {
System.out.println(line.replaceAll("example", "found!"));
}
in.close();
}
There are a lot of other tweaks you could make, but that's the core of the functionality you specified in your question.

File Merging in Java with .00x extension

I am currently practicing with splitting and merging a file. I found a piece of code on the web authored by a "krishna" with split and merge classes. The splitter worked like a charm, and I did some modifications to make it work the way I like it.
Here's the catch: I want the merger class to open the .00x files the splitter generates. But it is only limited to exactly eight .00x files, no more no less.
If only I could make it read all .00x files in the folder. I've been thinking long for a solution but I can't seem to generate one. I thought about making something that will scan the number of file with the .00x extension and make a loop based on it. Pls help me, or at least give me hints. Thank you! The code follows:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class MergeFile {
private static String FILE_NAME = JOptionPane.showInputDialog("Enter the file name");
public static void main(String[] args) {
File ofile = new File(FILE_NAME);
FileOutputStream fos;
FileInputStream fis;
short[] fileBytes;
int bytesRead = 0;
List<File> list = new ArrayList<File>();
list.add(new File(FILE_NAME+".001"));
list.add(new File(FILE_NAME+".002"));
list.add(new File(FILE_NAME+".003"));
list.add(new File(FILE_NAME+".004"));
list.add(new File(FILE_NAME+".005"));
list.add(new File(FILE_NAME+".006"));
list.add(new File(FILE_NAME+".007"));
list.add(new File(FILE_NAME+".008"));
try {
fos = new FileOutputStream(ofile,true);
for (File file : list) {
fis = new FileInputStream(file);
fileBytes = new byte[(int) file.length()];
bytesRead = fis.read(fileBytes, 0,(int) file.length());
assert(bytesRead == fileBytes.length);
assert(bytesRead == (int) file.length());
fos.write(fileBytes);
fos.flush();
fileBytes = null;
fis.close();
fis = null;
}
fos.close();
fos = null;
}catch (Exception exception){
exception.printStackTrace();
}
}
}
You can implement a FileFilter and pass it to the method File.listFiles() as shown below:
import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) {
final String FILE_NAME = "testfile";
/* The method listFiles returns all the files in the path
(I used "." to select the working directory).
This method accept a FileFilter as parameter. The file filter
decides what files to return. It is a simple interface with just
one method. */
File[] fileList = new File(".").listFiles(new FileFilter() {
#Override
public boolean accept(File pathname) {
/* Return true to include the file in the list */
return pathname.getName().startsWith(FILE_NAME);
}
});
List<File> list = Arrays.asList(fileList);
for (File f: list) {
System.out.println(f);
}
}
}
If you do not like to work with anonymous classes, you can just implement your FileFilter as a public class in its own file.
If you are using Java 8, then you can do it pretty easily using Files#list. Getting your list of Files starting with FILE_NAME and ending with .001, .002, .003, ... should work like this:
Path path = Paths.get(FILE_NAME);
Pattern pattern = Pattern.compile(Pattern.quote(path.getFileName().toString()) + "\\.\\d{3}");
List<File> list = Files.list(path.getParent())
.filter(f -> pattern.matcher(f.getFileName().toString()).matches())
.map(Path::toFile)
.collect(Collectors.toList());
This is just from the top of my head, I didn't test it as I had no .00x files lying around.
import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.regex.Pattern;
public class ListFiles {
private static final String BASE_DIR = "<your directory>";
private static final String FILE_EXT = ".*\\.[0-9]{3,3}";
private class FileFilter implements FilenameFilter {
private String ext;
public FileFilter(String ext) {
this.ext = ext;
}
public boolean accept(File dir, String name) {
return Pattern.matches(ext, name) ? true : false;
}
}
public static void main(String args[]) {
System.out.println(Arrays.toString(new ListFiles().listFile(BASE_DIR, FILE_EXT)));
}
public String[] listFile(String folder, String ext) {
File dir = new File(folder);
String[] list = dir.list(new FileFilter(ext));
return list;
}
}

Buffered reader not accessable by other methods

Sorry if this is obvious, I am inexperienced with Java. I have 2 methods, one that creates a BufferedReader, and one that processes it. However, the processing method can not access the BufferedReader, even though it is in a public method. Am I doing something wrong?
package textfiles;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class ReadFile {
private String path;
public ReadFile(String filePath) {
path = filePath;
}
public void Open() throws IOException {
FileReader read = new FileReader(path);
BufferedReader buff = new BufferedReader(read);
}
public String[] OpenFile() throws IOException {
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
int i;
for (i=0; i < numberOfLines; i++) {
textData[i] = buff.readLine();
}
buff.close();
return textData;
}
int readLines() throws IOException {
FileReader linedFile = new FileReader(path);
BufferedReader findLines = new BufferedReader(linedFile);
String lines;
int noLines = 0;
while ((lines = findLines.readLine()) != null) {
noLines++;
}
findLines.close();
return noLines;
}
}
Define BufferedReader at instance level just after declaring your path variable like
BufferedReader buff;
And in your method open, initialize it like
buff = new BufferedReader(read);
Your code should return compile time error as buff undefined variable. So declare it as instance variable and use it in any method directly.

Read from file inside Eclipse Project

I want to make an application that splits a big text file inside inputfolder into several small XML files to be put inside outputfolder.
This is project outline:
The following code works fine when it comes to getting a file from an outside folder, but when I modified it to read from a folder inside the project, it gave me this error:
Exception in thread "main" java.lang.NullPointerException
at com.zakaria.cut.XmlCutter.cut(XmlCutter.java:45)
at com.zakaria.cut.Main.main(Main.java:8)
[XmlCutter.java]
package com.zakaria.cut;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.MessageFormat;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
public class XmlCutter {
private static final String OUTPUT_FILE_NAME = "/file";
//private static String USER_HOME = System.getProperty("user.home");
private static final String INPUT_FOLDER = "../inputfolder";
private static String OUTPUT_FOLDER = "../outputfolder";
private static Logger LOG = Logger.getLogger("XmlCutter");
private static long COUNTER = 0;
public XmlCutter() {
super();
// TODO Auto-generated constructor stub
}
public void cut() {
Handler h = new ConsoleHandler();
h.setLevel(Level.FINE);
LOG.addHandler(h);
LOG.setLevel(Level.FINE);
File inputDir = new File(INPUT_FOLDER);
File[] filesInInputDir = inputDir.listFiles();
for (File f : filesInInputDir) {
if ((f.getName()).endsWith(".txt")) {
LOG.fine((MessageFormat.format(
"Found a text file {0}. Processing docs...",
f.getName())));
processFile(f);
}
}
}
private static void processFile(File f) {
StringBuilder out = new StringBuilder();
char prev = '#';
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(f), "UTF8"));
char[] buf = new char[1];
while (br.read(buf) >= 0) {
out.append(buf[0]);
if (prev == '<' && buf[0] == '?') {
LOG.finest((MessageFormat.format(
"Start of XML PI Found: {0}{1}", prev, buf[0])));
if (out.length() > 2) {
flushToFile(out.substring(0, out.length() - 2));
}
out.setLength(2);
}
prev = buf[0];
}
LOG.finest("Writing final file");
flushToFile(out.toString());
br.close();
} catch (IOException e) {
LOG.fine(e.getMessage());
}
LOG.fine(MessageFormat.format("Generated {0} XML Documents", COUNTER));
}
private static void flushToFile(String s) {
File f = new File(OUTPUT_FOLDER + OUTPUT_FILE_NAME + (++COUNTER)
+ ".xml");
LOG.finest(MessageFormat.format("Writing file: {0}", f.getName()));
try {
FileOutputStream fos = new FileOutputStream(f);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
osw.write(s);
osw.flush();
} catch (IOException e) {
LOG.fine(e.getMessage());
}
}
}
[Main.java]
package com.zakaria.cut;
public class Main {
public static void main(String[] args) {
XmlCutter cutter = new XmlCutter();
cutter.cut();
}
}
The problem, I guess, is definitely here:
private static final String INPUT_FOLDER = "../inputfolder";
private static String OUTPUT_FOLDER = "../outputfolder";
How can I fix it?
Do you know what folder the program is executing from? My guess is the relative links are pointing to the wrong spot? Have you tried hard coding the paths and see if they work? If they do you might have to look at the your execution folder and then change the relative paths accordingly?

JUnit: How to simulate System.in testing?

I have a Java command-line program. I would like to create JUnit test case to be able to simulate System.in. Because when my program runs it will get into the while loop and waits for input from users. How do I simulate that in JUnit?
Thanks
It is technically possible to switch System.in, but in general, it would be more robust not to call it directly in your code, but add a layer of indirection so the input source is controlled from one point in your application. Exactly how you do that is an implementation detail - the suggestions of dependency injection are fine, but you don't necessarily need to introduce 3rd party frameworks; you could pass round an I/O context from the calling code, for example.
How to switch System.in:
String data = "Hello, World!\r\n";
InputStream stdin = System.in;
try {
System.setIn(new ByteArrayInputStream(data.getBytes()));
Scanner scanner = new Scanner(System.in);
System.out.println(scanner.nextLine());
} finally {
System.setIn(stdin);
}
Based on #McDowell's answer and another answer that shows how to test System.out, I would like to share my solution to give an input to a program and test its output.
As a reference, I use JUnit 4.12.
Let's say we have this program that simply replicates input to output:
import java.util.Scanner;
public class SimpleProgram {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(scanner.next());
scanner.close();
}
}
To test it, we can use the following class:
import static org.junit.Assert.*;
import java.io.*;
import org.junit.*;
public class SimpleProgramTest {
private final InputStream systemIn = System.in;
private final PrintStream systemOut = System.out;
private ByteArrayInputStream testIn;
private ByteArrayOutputStream testOut;
#Before
public void setUpOutput() {
testOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(testOut));
}
private void provideInput(String data) {
testIn = new ByteArrayInputStream(data.getBytes());
System.setIn(testIn);
}
private String getOutput() {
return testOut.toString();
}
#After
public void restoreSystemInputOutput() {
System.setIn(systemIn);
System.setOut(systemOut);
}
#Test
public void testCase1() {
final String testString = "Hello!";
provideInput(testString);
SimpleProgram.main(new String[0]);
assertEquals(testString, getOutput());
}
}
I won't explain much, because I believe the code is readable and I cited my sources.
When JUnit runs testCase1(), it is going to call the helper methods in the order they appear:
setUpOutput(), because of the #Before annotation
provideInput(String data), called from testCase1()
getOutput(), called from testCase1()
restoreSystemInputOutput(), because of the #After annotation
I didn't test System.err because I didn't need it, but it should be easy to implement, similar to testing System.out.
There are a few ways to approach this. The most complete way is to pass in an InputStream while running the class under test which is a fake InputStream which passes simulated data to your class. You can look at a dependency injection framework (such as Google Guice) if you need to do this a lot in your code, but the simple way is:
public class MyClass {
private InputStream systemIn;
public MyClass() {
this(System.in);
}
public MyClass(InputStream in) {
systemIn = in;
}
}
Under test you would call the constructor that takes the input stream. You cloud even make that constructor package private and put the test in the same package, so that other code would not generally consider using it.
Try to refactor your code to use dependency injection. Instead of having your a method that uses System.in directly, have the method accept an InputStream as an argument. Then in your junit test, you'll be able to pass a test InputStream implementation in place of System.in.
You can write a clear test for the command line interface by using the TextFromStandardInputStream rule of the System Rules library.
public void MyTest {
#Rule
public final TextFromStandardInputStream systemInMock
= emptyStandardInputStream();
#Test
public void readTextFromStandardInputStream() {
systemInMock.provideLines("foo");
Scanner scanner = new Scanner(System.in);
assertEquals("foo", scanner.nextLine());
}
}
Full disclosure: I'm the author of that library.
You could create a custom InputStream and attach it to the System class
class FakeInputStream extends InputStream {
public int read() {
return -1;
}
}
And then use it with your Scanner
System.in = new FakeInputStream();
Before:
InputStream in = System.in;
...
Scanner scanner = new Scanner( in );
After:
InputStream in = new FakeInputStream();
...
Scanner scanner = new Scanner( in );
Although I think you should better to test how your class should work with the data read from the input stream and not really how it reads from there.
The problem with BufferedReader.readLine() is that it is a blocking method which waits for user input. It seems to me that you don't particularly want to simulate that (i.e. you want tests to be fast). But in a testing context it continually returns null at high speed during testing, which is irksome.
For a purist you can make the getInputLine below package-private, and mock it: easy-peezy.
String getInputLine() throws Exception {
return br.readLine();
}
... you'd have to make sure that you had a way of stopping (typically) a loop of user interaction with the app. You'd also have to cope with the fact that your "input lines" would always be the same until you somehow changed the doReturn of your mock: hardly typical of user input.
For a non-purist who wishes to make life easy for themselves (and produce readable tests) you could put all this stuff below in your app code:
private Deque<String> inputLinesDeque;
void setInputLines(List<String> inputLines) {
inputLinesDeque = new ArrayDeque<String>(inputLines);
}
private String getInputLine() throws Exception {
if (inputLinesDeque == null) {
// ... i.e. normal case, during app run: this is then a blocking method
return br.readLine();
}
String nextLine = null;
try {
nextLine = inputLinesDeque.pop();
} catch (NoSuchElementException e) {
// when the Deque runs dry the line returned is a "poison pill",
// signalling to the caller method that the input is finished
return "q";
}
return nextLine;
}
... in your test you might then go like this:
consoleHandler.setInputLines( Arrays.asList( new String[]{ "first input line", "second input line" }));
before triggering off the method in this "ConsoleHandler" class which needs input lines.
maybe like this (not tested):
InputStream save_in=System.in;final PipedOutputStream in = new PipedOutputStream(); System.setIn(new PipedInputStream(in));
in.write("text".getBytes("utf-8"));
System.setIn( save_in );
more parts:
//PrintStream save_out=System.out;final ByteArrayOutputStream out = new ByteArrayOutputStream();System.setOut(new PrintStream(out));
InputStream save_in=System.in;final PipedOutputStream in = new PipedOutputStream(); System.setIn(new PipedInputStream(in));
//start something that reads stdin probably in a new thread
// Thread thread=new Thread(new Runnable() {
// #Override
// public void run() {
// CoursesApiApp.main(new String[]{});
// }
// });
// thread.start();
//maybe wait or read the output
// for(int limit=0; limit<60 && not_ready ; limit++)
// {
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
in.write("text".getBytes("utf-8"));
System.setIn( save_in );
//System.setOut(save_out);
#Stefan Birkner, Thanks!
Modify Pom.xml
Ref:
https://stackoverflow.com/a/66127606/8317677
https://github.com/stefanbirkner/system-lambda/blob/master/pom.xml
https://github.com/stefanbirkner/system-lambda/blob/master/src/test/java/com/github/stefanbirkner/systemlambda/WithTextFromSystemInTest.java
<properties>
<system-lambda.version>1.2.1</system-lambda.version>
</properties>
<dependencies>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-lambda</artifactId>
<version>${system-lambda.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Add function code
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class SimpleProgram003 {
public static void main(String[] args) {
try{
String c;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
do{
c = in.readLine();
System.out.println(c);
String d = c;
}while(!c.equals("q"));
}catch(Exception e){
System.out.println("catch Exception");
}
}
}
Add test code
import static com.github.stefanbirkner.systemlambda.SystemLambda.*;
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* Unit test for simple App. JUnit 4.x.
*/
public class SimpleProgram003Test {
private final InputStream systemIn = System.in;
private final PrintStream systemOut = System.out;
private ByteArrayInputStream testIn;
private ByteArrayOutputStream testOut;
#Before
public void setUpOutput() {
testOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(testOut));
}
private void setInput(String data) {
testIn = new ByteArrayInputStream(data.getBytes());
System.setIn(testIn);
}
private String getOutput() {
return testOut.toString();
}
#After
public void restoreSystemInputOutput() {
System.setIn(systemIn);
System.setOut(systemOut);
}
#Test
public void testCase1() {
final String testString = "Hello 1\nq\n";
setInput(testString);
SimpleProgram003.main(new String[0]);
// String a = getOutput();
assertEquals("Hello 1\r\nq\r\n", getOutput());
}
#Test // Multiply inputs
public void testCase2() throws Exception {
withTextFromSystemIn(
"Input1",
"Input2",
"q",
"Input3"
).execute(() -> {
SimpleProgram003.main(new String[0]);
// String a = getOutput();
assertEquals("Input1\r\nInput2\r\nq\r\n", getOutput());
});
}
}

Categories