使用 Junit 测试 InputStream
Test InputStream with Junit
我正在尝试在我的 class "UserInput".
中测试函数 "DataFeatures"
无论我在测试中给出什么参数,它总是通过。
字段和构造函数
public @Getter class UserInput {
FileType type;
FileOperation operation;
SynchronizationMethod method;
String path;
private static InputReader in = new InputReader();
public UserInput() {
// Dont need to do anything
}
要测试的函数
void getDataFeatures() {
System.out.println("For encryption press 1");
System.out.println("For decryption press 0");
operation = FileOperation.fromInt(in.nextInt());
System.out.println("For a file choose 1");
System.out.println("For an entire directory choose 0");
type = FileType.fromInt(in.nextInt());
if (type == FileType.DIR) {
System.out.println("For sync press 1");
System.out.println("For async press 0");
method = SynchronizationMethod.fromInt(in.nextInt());
}
}
我的测试
public class UserInputTest {
UserInput UI;
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private final PrintStream oldStdOut = System.out;
private final PrintStream oldStdErr = System.err;
private final InputStream oldStdIn = System.in;
@Before
public void initlize(){
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
UI = new UserInput();
}
@Test
public void getDataFeaturesTest() {
String data = "0" + "\n0" + "\n0";
System.setIn(new ByteArrayInputStream(data.getBytes()));
UI.getDataFeatures();
System.out.println(UI.getOperation());
assertThat(UI.getOperation(), is(equalTo(FileOperation.decryption)));
assertThat(UI.getType(), is(equalTo(FileType.FILE)));
assertThat(UI.getMethod(), is(equalTo(SynchronizationMethod.SYNC)));
}
@After
public void cleanUpStreams() {
System.setOut(oldStdOut);
System.setErr(oldStdErr);
System.setIn(oldStdIn);
}
}
注1:FileOperation、FileType和SynchronizationMethod都是取1或0的枚举。
同步方法示例:
public enum SynchronizationMethod {
SYNC(1), ASYNC(0);
private int method;
private SynchronizationMethod(int meth) {
this.method = meth;
}
public static SynchronizationMethod fromInt(int meth) {
for (SynchronizationMethod SM : SynchronizationMethod.values()) {
if (SM.method == meth) {
return SM;
}
}
throw new IllegalArgumentException("No constant with method " + meth + " found");
}
public String toString(){
if (method == 1){
return "Sync";
}
else if(method == 0){
return "ASync";
}
else{
throw new IllegalArgumentException("No constant with method " + method + " found in toString");
}
}
}
解决方案
问题出在构造函数中的 class InputReader。
public InputReader() {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
}
这个函数中的 reader 和 Junit 函数中的 Input Stream 如评论中所建议的那样断开连接
猜测 这里 - 你的 UserInput class 说:
private static InputReader in = new InputReader();
而你的测试用例说:
String data = "0" + "\n0" + "\n0";
System.setIn(new ByteArrayInputStream(data.getBytes()));
换句话说:可能断开连接。根据 InputReader
背后的实施,您可能只是从错误的来源阅读。
我正在尝试在我的 class "UserInput".
中测试函数 "DataFeatures"无论我在测试中给出什么参数,它总是通过。
字段和构造函数
public @Getter class UserInput {
FileType type;
FileOperation operation;
SynchronizationMethod method;
String path;
private static InputReader in = new InputReader();
public UserInput() {
// Dont need to do anything
}
要测试的函数
void getDataFeatures() {
System.out.println("For encryption press 1");
System.out.println("For decryption press 0");
operation = FileOperation.fromInt(in.nextInt());
System.out.println("For a file choose 1");
System.out.println("For an entire directory choose 0");
type = FileType.fromInt(in.nextInt());
if (type == FileType.DIR) {
System.out.println("For sync press 1");
System.out.println("For async press 0");
method = SynchronizationMethod.fromInt(in.nextInt());
}
}
我的测试
public class UserInputTest {
UserInput UI;
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private final PrintStream oldStdOut = System.out;
private final PrintStream oldStdErr = System.err;
private final InputStream oldStdIn = System.in;
@Before
public void initlize(){
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
UI = new UserInput();
}
@Test
public void getDataFeaturesTest() {
String data = "0" + "\n0" + "\n0";
System.setIn(new ByteArrayInputStream(data.getBytes()));
UI.getDataFeatures();
System.out.println(UI.getOperation());
assertThat(UI.getOperation(), is(equalTo(FileOperation.decryption)));
assertThat(UI.getType(), is(equalTo(FileType.FILE)));
assertThat(UI.getMethod(), is(equalTo(SynchronizationMethod.SYNC)));
}
@After
public void cleanUpStreams() {
System.setOut(oldStdOut);
System.setErr(oldStdErr);
System.setIn(oldStdIn);
}
}
注1:FileOperation、FileType和SynchronizationMethod都是取1或0的枚举。
同步方法示例:
public enum SynchronizationMethod {
SYNC(1), ASYNC(0);
private int method;
private SynchronizationMethod(int meth) {
this.method = meth;
}
public static SynchronizationMethod fromInt(int meth) {
for (SynchronizationMethod SM : SynchronizationMethod.values()) {
if (SM.method == meth) {
return SM;
}
}
throw new IllegalArgumentException("No constant with method " + meth + " found");
}
public String toString(){
if (method == 1){
return "Sync";
}
else if(method == 0){
return "ASync";
}
else{
throw new IllegalArgumentException("No constant with method " + method + " found in toString");
}
}
}
解决方案
问题出在构造函数中的 class InputReader。
public InputReader() {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
}
这个函数中的 reader 和 Junit 函数中的 Input Stream 如评论中所建议的那样断开连接
猜测 这里 - 你的 UserInput class 说:
private static InputReader in = new InputReader();
而你的测试用例说:
String data = "0" + "\n0" + "\n0";
System.setIn(new ByteArrayInputStream(data.getBytes()));
换句话说:可能断开连接。根据 InputReader
背后的实施,您可能只是从错误的来源阅读。