-1
我有一個服務器客戶端,它使用發送消息的PrintWriter和傳入消息的BufferedReader從服務器套接字發送和接收信息。我試圖通過BufferedReader向客戶端發送字符串並從PrintWriter讀取返回的輸出來在JUnit中測試沒有服務器連接的客戶端。從PrintWriter和BufferedReader發送和讀取字符串
class Client{
private BufferedReader incomingMessage;
private PrintWriter outgoingMessage;
private StringWriter output;
//Constructor for testing without server connection
public Client(){
output = new StringWriter();
outgoingMessage = new PrintWriter(output);
incomingMessage = new BufferedReader(new InputStreamReader(System.in));
}
//Methods for processing incoming messages and sending responses are
//omitted
//responses are sent using outgoingMessage.println("msg");
public void sendStringToInputStream(String msg){
incomingMessage = new BufferedReader(new StringReader(msg));
}
public String getOutputAsString(){
return output.toString();
}
}
這是我正在運行的測試。
public class ServerMessageTest {
private Client testClient;
private String output;
@Before
public void setUp(){
testClient = new Client();
}
@Test
public void testClientOutputMessage(){
testClient.sendStringToInputStream("GAME A OVER SEND OUTCOME");
output = testClient.getOutputAsString();
String testString = "GAME A OVER PLAYER 1 0 PLAYER 2 0";
Assert.assertEquals(testString, output.toString());
}
}
測試失敗顯示此:
org.junit.ComparisonFailure:
Expected :GAME A OVER PLAYER 1 0 PLAYER 2 0
Actual :
所以這是與讀取輸出消息或設置輸入消息的問題。我對IO的東西有點新,所以如果有人能指出我做錯了什麼,我會非常感激!
這實際上不是我的代碼。我的部分任務是擴展和測試同學代碼的功能。我試圖理解他們在這裏做什麼,但我不能。實際代碼中有方法通過BufferedReader和PrintWriter打印和讀取行。我不能發佈這些,因爲我們不被允許。我只需要弄清楚如何從PrintWriter捕獲輸出流,並通過BufferedReader強制輸入而不連接到套接字。我知道他們是輸入/輸出流。只需要幫助訪問它們。 –
它的代碼甚至沒有什麼相關性。你發佈的代碼有我提到的缺陷。如果這不是真正的代碼或所有相關代碼,或者您詢問的實際問題並未完全包含在您的問題中,那麼這是完全浪費時間。 – EJP