Bonjour,
J'ai une classe de communication TCP et je souhaiterais la tester avec Mockito.
Auriez vous des idées ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 public TCPCom(Socket socket, Messenger messenger) { this.serverThread = new Thread(this); this.startThread(this.serverThread); this.socket = socket; this.messenger = messenger; } /** * @return the messenger. */ public Messenger getMessenger() { return this.messenger; } /** * @param messenger the messenger to set. */ public void setMessenger(Messenger messenger) { this.messenger = messenger; } /** * @return the socket. */ public Socket getSocket() { return this.socket; } /** * @param socket the socket to set. */ public void setSocket(Socket socket) { this.socket = socket; } /** * Method to start the thread. * @param th thread of the class */ private void startThread(Thread th) { th.start(); } /** * Method to send a command to the client. * @param command to send */ public synchronized void sendCommand(String command) { try { OutputStreamWriter osw = new OutputStreamWriter(this.socket.getOutputStream()); this.bufferedOutputReader = new BufferedWriter(osw); this.outToClient = new PrintWriter(this.bufferedOutputReader,true); this.outToClient.println(command); System.out.println("Commande envoye : " + command); } catch (IOException e) { e.printStackTrace(); } } /** * Method to receive an command from the client. * @throws IOException throws IOExeption */ public void receiveCommand() throws IOException { Thread current = Thread.currentThread(); InputStream input = this.socket.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input)); String buffer; try { System.out.println("TCP com is running ..."); while (this.serverThread == current) { try { if((buffer = bufferedReader.readLine()) != null) { System.out.println("Commande recu : " + buffer); this.messenger.interpretMessage(buffer); } }catch (IOException e) { } try { Thread.sleep(SLEEP_TIME); } catch (InterruptedException e) { e.printStackTrace(); } } } finally { input.close(); this.socket.close(); } } @Override public void run() { try { this.receiveCommand(); } catch (IOException e) { e.printStackTrace(); } }
Partager