java:调度程序中的单元测试 lambda 表达式

java: unit test lambda expression inside dispatcher

我有一个class:

public class RequestHandler implements HttpHandler {
  public void handleRequest(HttpServerExchange serverContext) throws Exception {
    serverContext.dispatch(() -> serverContext.getRequestReceiver()
        .receiveFullBytes((httpServerExchange, reqBytes) -> {

          // business logic along with few function call

        }
      )
    );

  }
} 

我想写一个单元测试用例来测试我的业务逻辑。我不确定如何在调度员内部使用 2 级 lambda 表达式来做到这一点?有人可以推荐一个编写测试用例的好方法吗?

我知道我们可以将业务逻辑移动到新的 class 并可以对其进行测试(我想它设计得更好)但很想知道它是否是某些遗留代码的一部分或我们不能做的事情改变,我们如何测试它?

假设您在业务逻辑中的某个地方将收到的消息(或您对它所做的任何操作)转发到其他地方,您可以像往常一样测试您的代码。

请注意 HttpServerExchange 是最终的 class,因此您需要使用支持 final 模拟的 Mockito 版本 - 您必须启用它,如所述 here.

要绕过 lambda 表达式,您需要使用 thenAnswer or doAnswer 手动触发正确接口方法的调用。

一个简单的例子可能是这样的:

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;

import io.undertow.io.Receiver;
import io.undertow.io.Receiver.FullBytesCallback;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;

@ExtendWith(MockitoExtension.class)
public class RequestHandlerTest {

    static class BuisnessLogic {
        public void someMethod(HttpServerExchange httpServerExchange, byte[] reqBytes) {
        }
    }

    static class RequestHandler implements HttpHandler {

        BuisnessLogic logic;

        public void handleRequest(HttpServerExchange serverContext) throws Exception {
            serverContext.dispatch(
                () -> serverContext.getRequestReceiver().receiveFullBytes(
                    (httpServerExchange, reqBytes) -> {
                        logic.someMethod(httpServerExchange, reqBytes);
                    }
                )
            );
        }
    }

    @Mock
    BuisnessLogic logic;

    @InjectMocks
    RequestHandler handler;

    @Test
    public void test() throws Exception {

        byte[] message = new byte[] {1,2,3};
        HttpServerExchange serverContext = Mockito.mock(HttpServerExchange.class);

        // 1st lambda
        Mockito.when(serverContext.dispatch(Mockito.any(Runnable.class)))
               .thenAnswer((Answer<HttpServerExchange>) invocation -> {

            Runnable runnable = invocation.getArgument(0);
            runnable.run();

            return serverContext;
        });

        // 2nd lambda
        Receiver receiver = Mockito.mock(Receiver.class);
        Mockito.doAnswer((Answer<Void>) invocation -> {

            FullBytesCallback callback = invocation.getArgument(0);
            callback.handle(serverContext, message);

            return null;

        }).when(receiver).receiveFullBytes(Mockito.any(FullBytesCallback.class));

        Mockito.when(serverContext.getRequestReceiver()).thenReturn(receiver);

        // class under test - method invocation
        handler.handleRequest(serverContext);

        // buisness logic call verification
        ArgumentCaptor<HttpServerExchange> captor1 = ArgumentCaptor.forClass(HttpServerExchange.class);
        ArgumentCaptor<byte[]> captor2 = ArgumentCaptor.forClass(byte[].class);

        Mockito.verify(logic).someMethod(captor1.capture(), captor2.capture());

        Assertions.assertEquals(serverContext, captor1.getValue());
        Assertions.assertEquals(message, captor2.getValue());
    }
}

正如其他人已经提到的那样,您应该只对遗留代码使用该方法。

一个简单的重构可以将您需要测试的整个部分推送到它自己的方法中,在上面的示例中,它只是业务逻辑本身。

没有明确需要自己测试 undertow 框架。