This commit is contained in:
trochas
2025-12-12 09:00:43 +01:00
parent 5869506f40
commit d9e1841216
2 changed files with 53 additions and 0 deletions

View File

@@ -1,6 +1,9 @@
package fr.istic.vv;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import java.util.ArrayList;
import java.util.Arrays;
@@ -8,8 +11,51 @@ import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class TLSSocketFactoryTestMocks {
@Test
public void preparedSocket_NullProtocols(){
TLSSocketFactory f = new TLSSocketFactory();
SSLSocket socket = Mockito.mock(SSLSocket.class);
when(socket.getSupportedProtocols()).thenReturn(null);
when(socket.getEnabledProtocols()).thenReturn(null);
f.prepareSocket(socket);
verify(socket, never()).setEnabledProtocols(any());
}
@Test
public void typical() {
TLSSocketFactory f = new TLSSocketFactory();
SSLSocket socket = Mockito.mock(SSLSocket.class);
when(socket.getSupportedProtocols()).thenReturn(shuffle(new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"}));
when(socket.getEnabledProtocols()).thenReturn(new String[]{"SSLv3", "TLSv1"});
f.prepareSocket(socket);
verify(socket).setEnabledProtocols(
eq(new String[] {"TLSv1.2", "TLSv1.1", "TLSv1", "SSLv3"})
);
}
private String[] shuffle(String[] in) {
List<String> list = new ArrayList<String>(Arrays.asList(in));
Collections.shuffle(list);
return list.toArray(new String[0]);
}
}

View File

@@ -9,3 +9,10 @@ Rewrite these tests with the help of Mockito.
The initial tests fail to completely test the `TLSSockeetFactory`. In fact, if we *entirely* remove the code inside the body of `prepareSocket` no test case fails.
Propose a solution to this problem in your new Mockito-based test cases.
Aucun test n'échoue quand on supprime le contenu de prepareSocket() car les tests ne vérifient les conditions seulement si setEnabledProtocols se lance, or si prepareSocket() est vide setEnabledProtocols n'est jamais appelé, donc ni le fail(), ni le assertTrue() n'est exécuté. Le test passe donc.
avec les Mock on utilise verify. Si setEnabledProtocols n'est jamais appelé alors c'est aussi une erreur