'JUnit 4 testing for a class that separates file
I'm trying to write a test class with jUnit 4 to test Separating class. Separating class has 3 methods to open a file, separate its content into 3 files and close the file. The SeparatingTest doesn't work. It separates the content of the file but it doesn't pass the test. I am new to jUnit test. Can you help me with that please? separating class:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.*;
import java.util.*;
public class Separating {
public static BufferedReader input;
public static PrintWriter output14;
public static PrintWriter output15;
public static PrintWriter output16;
public static void openFiles() throws FileNotFoundException, IOException {
input= new BufferedReader(new FileReader("numbers.txt"));
output14 = new PrintWriter(new FileOutputStream("numbers14.txt"));
output15 = new PrintWriter(new FileOutputStream("numbers15.txt"));
output16 = new PrintWriter(new FileOutputStream("numbers16.txt"));
}
public void separate(BufferedReader input, PrintWriter output14,
PrintWriter output15, PrintWriter output16) throws IOException, InvalidUoBException {
String line;
while ((line = input.readLine()) != null) {
if (line.startsWith("14")) {
output14.write(line + "\n");
count14++;
} else if (line.startsWith("15")) {
output15.write(line + "\n");
count15++;
} else if (line.startsWith("16")) {
output16.write(line + "\n");
count16++;
} else {
throw new InvalidUoBException("not 14, 15, 16");
}
}
}
public static void closeFiles() throws IOException {
input.close();
output14.close();
output15.close();
output16.close();
}
public static void main(String[] args) throws IOException {
try {
Separating br = new Separating();
openFiles();
br.separate(input, output14, output15, output16);
closeFiles();
System.out.println("Successfuly separated the file");
} catch (InvalidUoBException e) {
System.out.println("UoB number does not start with 14, 15 or 16!");
} catch (FileNotFoundException e) {
System.out.println("File was not found");
System.out.println("or could not be opened.");
} catch (IOException e) {
System.out.println("Error reading from file.");
}
}
}
test class:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import static junit.framework.TestCase.assertEquals;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
public class SeparatingTest extends Separating {
public SeparatingTest() {
}
public static BufferedReader inputTest;
public static PrintWriter outputTest14;
public static PrintWriter outputTest15;
public static PrintWriter outputTest16;
public static BufferedReader inputTest14;
public static BufferedReader inputTest15;
public static BufferedReader inputTest16;
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() throws FileNotFoundException {
inputTest = new BufferedReader(new FileReader("testnumbers.txt"));
outputTest14 = new PrintWriter(new FileOutputStream("testnumbers14.txt"));
outputTest15 = new PrintWriter(new FileOutputStream("testnumbers15.txt"));
outputTest16 = new PrintWriter(new FileOutputStream("testnumbers16.txt"));
inputTest14 = new BufferedReader(new FileReader("testnumbers14.txt"));
inputTest15 = new BufferedReader(new FileReader("testnumbers15.txt"));
inputTest16 = new BufferedReader(new FileReader("testnumbers16.txt"));
}
@After
public void tearDown() throws IOException {
inputTest.close();
outputTest14.close();
outputTest15.close();
outputTest16.close();
inputTest14.close();
inputTest15.close();
inputTest16.close();
}
@Test
public void testSeparate() throws Exception {
/*testnumbers.txt: 14451024
15752601
16148923
*/
Separating br200 = new Separating();
br200.separateStudents(inputTest, outputTest14, outputTest15, outputTest16);
assertEquals(inputTest14.readLine(), "18451024");
assertEquals(inputTest15.readLine(), "15752601");
assertEquals(inputTest16.readLine(), "16148923");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
