Ish*_*raD 1 java junit mockito
这是我的 java 线程运行方法,我想为此方法编写单元测试。但是使用无限循环我做不到。很高兴有人能帮我解决这个问题。
public void run() {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line = null;
boolean oneTime = true;
loadDescription();
while (true) {
try {
if (oneTime) {
System.out.println("press enter to get the console...............");
oneTime = false;
}
line = in.readLine();
if (line != null) {
processInput(line);
}
} catch (Exception e) {
logger.error("Error occurred while processing console");
logger.error(e.getMessage(), e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是 loadDescription() 方法
private void loadDescription() {
BufferedReader in = null;
StringBuffer stringBuffer = null;
String str = null;
try {
stringBuffer = new StringBuffer();
in = new BufferedReader(new FileReader(IConstants.MANUAL_FILE));
str = in.readLine();
while (str != null) {
stringBuffer.append(str + "\n");
str = in.readLine();
}
dfixDescription = stringBuffer.toString();
} catch (Exception e) {
logger.error(e.getMessage(), e); //To change body of catch statement use File | Settings | File Templates.
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
logger.error(e.getMessage(), e); //To change body of catch statement use File | Settings | File Templates.
}
}
}
Run Code Online (Sandbox Code Playgroud)
和
private void processInput(String line) {
line = line.trim();
logger.info("Admin Message received: " + line);
if ("?".equalsIgnoreCase(line) || "".equals(line)) {
printDescription();
} else if (line.toUpperCase().startsWith(AdminCommands.RELOAD)) {
loadDescription();
printDescription();
} else if (line.toUpperCase().startsWith(AdminCommands.EXIT)) {
adminManager.stopDFIXRouter();
logger.debug("Closing Application.....");
logger.debug("Closed.");
System.exit(0);
} else if (line.toUpperCase().startsWith(AdminCommands.RESET_OUT_SEQUENCE)) {
try {
String sessionIdentifier = line.split(",")[1].trim();
int seqNo = Integer.parseInt((line.split(",")[2]).trim());
adminManager.resetOutSequence(sessionIdentifier, seqNo);
} catch (Exception e) {
logger.error(e.getMessage(), e); //To change body of catch statement use File | Settings | File Templates.
}
} else if (line.toUpperCase().startsWith(AdminCommands.RESET_IN_SEQUENCE)) {
try {
String sessionIdentifier = line.split(",")[1].trim();
int seqNo = Integer.parseInt((line.split(",")[2]).trim());
adminManager.resetInSequence(sessionIdentifier, seqNo);
} catch (Exception e) {
logger.error(e.getMessage(), e); //To change body of catch statement use File | Settings | File Templates.
}
} else if (line.toUpperCase().startsWith(AdminCommands.CONNECT)) {
String sessionIdentifier = line.split(",")[1].trim();
adminManager.connectSession(sessionIdentifier);
} else if (line.toUpperCase().startsWith(AdminCommands.DISCONNECT)) {
String sessionIdentifier = line.split(",")[1].trim();
adminManager.disconnectSession(sessionIdentifier);
} else if (line.toUpperCase().startsWith(AdminCommands.ACTIVATE)) {
adminManager.startDFIXRouter();
} else if (line.toUpperCase().startsWith(AdminCommands.PASSIVATE)) {
adminManager.stopDFIXRouter();
} else if (line.toUpperCase().startsWith(AdminCommands.RUN_EOD)) {
try {
String sessionIdentifier = line.split(",")[1].trim();
adminManager.runEod(sessionIdentifier);
} catch (Exception e) {
logger.error(e.getMessage(), e); //To change body of catch statement use File | Settings | File Templates.
}
} else if (line.toUpperCase().startsWith(AdminCommands.SHOW_STATUS)) {
adminManager.showStatus();
} else {
System.out.println("Usage: type ? for help");
}
System.out.print(">");
}
Run Code Online (Sandbox Code Playgroud)
这些是运行方法的相关方法。以下是我的测试方法
@Test
public void run_activate() throws Exception{
String data = "activate";
InputStream in = new ByteArrayInputStream(data.getBytes());
System.setIn(in);
PowerMockito.mockStatic(AdminManager.class);
PowerMockito.when(AdminManager.getInstance()).thenReturn(adminManagerTest);
Mockito.doNothing().when(adminManagerTest).startDFIXRouter();
dfixrtrAdminTest.run();
Mockito.verify(adminManagerTest, Mockito.times(1)).startDFIXRouter();
}
Run Code Online (Sandbox Code Playgroud)
这有什么问题。当我运行测试方法时,它不会停止,我无法验证是否调用了所需的方法来处理这个问题。
| 归档时间: |
|
| 查看次数: |
10643 次 |
| 最近记录: |