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

时间:2019-10-09 10:35:49

标签: java netty

我正在使用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;
    }
    }

2 个答案:

答案 0 :(得分:2)

io.netty.util.internal.PlatformDependent类中没有allocateUninitializedArray方法,因此,在您的类路径中,还有一个包含该类的jar,但是包含版本,因此该类的代码将是不同

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

因此,请检查项目的依赖关系树。 很可能您还有另一个依赖项,它具有不同版本的netty-common作为传递性依赖项。 排除错误的一个,您就可以完成。

答案 1 :(得分:0)

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

将此依赖项添加到pom.xml文件中。