如何解决我的测试项目中的 io.netty 错误

How do I solve an io.netty error in my testing project

我正在使用 Java 设置后端测试。 当 运行 我的测试出现以下错误时:

java.lang.NoSuchMethodError: io.netty.util.internal.PlatformDependent.allocateUninitializedArray(I)[B

我的 pom 文件包含以下与 Netty 相关的依赖项:

    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-transport</artifactId>
        <version>4.1.36.Final</version>
    </dependency>

我的代码本身如下所示:

import cucumber.api.java.en.And;
import org.mockserver.client.MockServerClient;
import org.mockserver.matchers.Times;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class PdfGenerateStep {

    @Autowired
    private MockServerClient mockServerClient;

    @And("Pdf {string} is generated")
    public void generatePDF(String pdfFile) {

        HttpRequest httpRequest = new HttpRequest();
        httpRequest.withPath("/pdf-service/doc/request")
            .withHeader("template", "TEST")
            .withHeader("docFormat", "pdf")
            .withHeader("fromParty", "PDFGEN")
            .withHeader("APPLICATION", "App")
            .withMethod("POST");
        HttpResponse httpResponse = new HttpResponse();
        httpResponse.withStatusCode(200);
        httpResponse.withBody(readPdfFile(pdfFile));

        mockServerClient.when(httpRequest, Times.once()).respond(httpResponse);
    }

    private byte[] readPdfFile(String file) {
        try {
            Path path = Paths.get(getClass().getClassLoader().getResource(file).toURI());

            return Files.readAllBytes(path);

        } catch (URISyntaxException | IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    }

io.netty.util.internal.PlatformDependent class 中没有 allocateUninitializedArray 方法,所以在你的 classpath 中有另一个包含这个 class 的 jar,但是 版本,因此 class 的代码会有所不同

io.netty.util.internal.PlatformDependentclass可以在netty-common中找到,它是netty-transport的传递依赖

所以,检查你的项目的依赖关系树。 很可能您有另一个依赖项,它具有不同版本的 netty-common 作为传递依赖项。 剔除错误就大功告成

    <dependency>
     <groupId>com.corundumstudio.socketio</groupId>
     <artifactId>netty-socketio</artifactId>
     <version>1.7.13</version>
    </dependency>

在您的 pom.xml 文件中添加此依赖项。