I am going crazy here so bear with me... We are using Fitnesse (with DbFit framework / based on FIT) to automatise some tests in which we run some shell commands. We have a fixture which connects to the linux server, runs the command and returns the results (see bellow)
class SSHConnection {
private static final String DEFAULT_KEY = "~/.ssh/id_rsa";
private String host;
private String password;
private int port;
private String privateKey;
private Session session;
private String user;
/** Creates a JSch object and open a connection with a remote server using the values set in the class variables.
*/
public void connect() {.........}
/**
* Executes a command in the remote server using the connection opened by connect().
* @param command command to execute in the remote server
* @return the output result of the command as String
*/
public String execute(String command) {
logger.info("Executing command: " + command);
String result;
try {
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
channelExec.setCommand(command);
// Open exec channel
channelExec.connect();
InputStream stream = channelExec.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuilder output = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
result = output.toString();
// Close exec channel
channelExec.disconnect();
logger.debug("Command executed successfully: " + result);
} catch (JSchException | IOException e) {
logger.error("Error executing command: " + e.getMessage());
e.printStackTrace();
return "";
}
return result;
}}
So I'm expecting whatever get's displayed on the shell after running the command to be returned (as a string) and compared to whatever my test in fitnesse requires.
Fitnesse catches the results but always fails the comparison and I don't know why (I only added the sed command to remove the whitespaces, but still the comparison fails!!
I feel like Fitnesse is mocking me showing me the same value for expected, actual and diff. Is it and encoding issue? is it a java type issue? How does check work?
Edit: I even tried running the shell command twice and saving the result the first time and then set it as the expected results. It still fails.
|set | VarAddress | run command | cat AddressNoSpaces.txt |
|check| run command | cat AddressNoSpaces.txt | @{VarAddress} |
OK problem solved, it seems that the shell command output added a new line char which fitnesse did not like. I changed that java class to strip the return value from it's last char and it's working.