Code example for DatagramSocket

2
    @Test 
    public void createDatagram() throws IOException { 
 
        DatagramSocket socket1 = new DatagramSocket();
        DatagramSocket socket2 = new DatagramSocket();
        DatagramSocket socket3 = new DatagramSocket();
        DatagramSocket socket4 = new DatagramSocket();
        InetAddress localhost = InetAddress.getLocalHost();
        SocketAddress socketAddress = new InetSocketAddress(localhost, 1);
 
        when(this.factory.createDatagramSocket("test")).thenReturn(socket1);
        when(this.factory.createDatagramSocket("test", 1)).thenReturn(socket2);
        when(this.factory.createDatagramSocket("test", socketAddress)).thenReturn(socket3);
        when(this.factory.createDatagramSocket("test", 1, localhost)).thenReturn(socket4);
 
        DatagramSocket result1 = this.subject.createDatagramSocket("test");
        DatagramSocket result2 = this.subject.createDatagramSocket("test", 1);
        DatagramSocket result3 = this.subject.createDatagramSocket("test", socketAddress);
        DatagramSocket result4 = this.subject.createDatagramSocket("test", 1, localhost);
 
        verify(this.manager.getUnnamedRegistry()).registerSocket(socket1);
        verify(this.manager.getUnnamedRegistry()).registerSocket(socket2);
        verify(this.manager.getUnnamedRegistry()).registerSocket(socket3);
        verify(this.manager.getUnnamedRegistry()).registerSocket(socket4);
 
        assertSame(socket1, result1);
        assertSame(socket2, result2);
        assertSame(socket3, result3);