HungryBird
HungryBird

Reputation: 1147

write a test for WebSocketHandlerDecorator - Delegate must not be null

I want to write a test to test my WebSocketHandlerDecorator, but I encouter some problems.

here is the error:

java.lang.IllegalArgumentException: Delegate must not be null

at org.springframework.util.Assert.notNull(Assert.java:193) at org.springframework.web.socket.handler.WebSocketHandlerDecorator.(WebSocketHandlerDecorator.java:42)

import org.springframework.util.Assert;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;  

public class MyWebSocketHandlerDecorator extends WebSocketHandlerDecorator {

    public MySocketHandlerDecorator(WebSocketHandler delegate) {
        super(delegate);
    }

    @Override
    public void handleMessage(final WebSocketSession session, final WebSocketMessage<?> message) throws Exception {

        final TextMessage MyMessage = (TextMessage) message;
        super.handleMessage(session, MyMessage);
    }
}

here is my test case:

public class MyWebSocketHandlerDecpratorTest {

    @Mock
    private WebSocketSession session;

    @Mock
    WebSocketHandler delegate;

    @Spy
    private WebSocketHandlerDecorator webSocketHandlerDecorator = new WebSocketHandlerDecorator(delegate);

    @InjectMocks
    MyWebSocketHandlerDecorator myWebSocketHandlerDecorator;

    private TextMessage message;

    @Before
    public void set_up(){
        MockitoAnnotations.initMocks(this);
        message = new TextMessage("Test Message".getBytes());
    }

    @Test
    public void handleMessage()throws Exception{
        myWebSocketHandlerDecorator.handleMessage(session, message);
        verify(webSocketHandlerDecorator, times(1)).handleMessage(session, message);
    }
}

Can anyone help me correct my test and figure out what issue it has?

public WebSocketHandlerDecorator(WebSocketHandler delegate) {
    Assert.notNull(delegate, "Delegate must not be null");
    this.delegate = delegate;
}

Upvotes: 1

Views: 1078

Answers (1)

Lino
Lino

Reputation: 19926

You have to setup your @Spy in the @Before method, because at the time the class is created, the mocks are not yet initialized:

public class MyWebSocketHandlerDecpratorTest {
    @Mock
    private WebSocketSession session;

    @Mock
    WebSocketHandler delegate;

    private WebSocketHandlerDecorator webSocketHandlerDecorator;

    @InjectMocks
    MyWebSocketHandlerDecorator myWebSocketHandlerDecorator;

    private TextMessage message;

    @Before
    public void set_up(){
        MockitoAnnotations.initMocks(this);
        webSocketHandlerDecorator = Mockito.spy(new WebSocketHandlerDecorator(delegate));
        message = new TextMessage("Test Message".getBytes());
    }

    @Test
    public void handleMessage()throws Exception{
        myWebSocketHandlerDecorator.handleMessage(session, message);
        verify(webSocketHandlerDecorator, times(1)).handleMessage(session, message);
    }
}

Upvotes: 1

Related Questions