Mockserver fails to read request: unknown message format

484 views Asked by At

I need to test some REST client. For that purpose I'm using org.mockserver.integration.ClientAndServer

I start my server. Create some expectation. After that I mock my client. Run this client. But when server receives request I see in logs:

14:00:13.511 [MockServer-EventLog0] INFO org.mockserver.log.MockServerEventLog - received binary request:

  505249202a20485454502f322e300d0a0d0a534d0d0a0d0a00000604000000000000040100000000000408000000000000ff0001

14:00:13.511 [MockServer-EventLog0] INFO org.mockserver.log.MockServerEventLog - unknown message format

  505249202a20485454502f322e300d0a0d0a534d0d0a0d0a00000604000000000000040100000000000408000000000000ff0001

This is my test:

@RunWith(PowerMockRunner.class)
@PrepareForTest({NrfClient.class, SnefProperties.class})
@PowerMockIgnore({"javax.net.ssl.*"})
@TestPropertySource(locations = "classpath:test.properties")
public class NrfConnectionTest {
    private String finalPath;
    private UUID uuid = UUID.fromString("8d92d4ac-be0e-4016-8b2c-eff2607798e4");

    private ClientAndServer mockServer;

    @Before
    public void startMockServer() {
        mockServer = startClientAndServer(8888);
    }

    @After
    public void stopServer() {
        mockServer.stop();
    }

    @Test
    public void NrfRegisterTest() throws Exception {
        //create some expectation
        new MockServerClient("127.0.0.1", 8888)
                .when(HttpRequest.request()
                        .withMethod("PUT")
                        .withPath("/nnrf-nfm/v1/nf-instances/8d92d4ac-be0e-4016-8b2c-eff2607798e4"))
                .respond(HttpResponse.response().withStatusCode(201));

        //long preparations and mocking the NrfClient (client that actually make request)
        //NrfClient is singleton, so had to mock a lot of methods.
        PropertiesConfiguration config = new PropertiesConfiguration();
        config.setAutoSave(false);
        File file = new File("test.properties");
        if (!file.exists()) {
            String absolutePath = file.getAbsolutePath();
            finalPath = absolutePath.substring(0, absolutePath.length() - "test.properties".length()) + "src\\test\\resources\\test.properties";
            file = new File(finalPath);
        }
        try {
            config.load(file);
            config.setFile(file);
        } catch(ConfigurationException e) {
            LogUtils.warn(NrfConnectionTest.class, "Failed to load properties from file " + "classpath:test.properties", e);
        }
        SnefProperties spyProperties = PowerMockito.spy(SnefProperties.getInstance());

        PowerMockito.doReturn(finalPath).when(spyProperties, "getPropertiesFilePath");
        PowerMockito.doReturn(config).when(spyProperties, "getProperties");
        PowerMockito.doReturn(config).when(spyProperties, "getLastUpdatedProperties");
        NrfConfig nrfConfig = getNrfConfig();
        NrfClient nrfClient = PowerMockito.spy(NrfClient.getInstance());
        SnefAddressInfo snefAddressInfo = new SnefAddressInfo("127.0.0.1", "8080");
        PowerMockito.doReturn(nrfConfig).when(nrfClient, "loadConfiguration", snefAddressInfo);
        PowerMockito.doReturn(uuid).when(nrfClient, "getUuid");

        Whitebox.setInternalState(SnefProperties.class, "instance", spyProperties);
        nrfClient.initialize(snefAddressInfo);

        //here the client makes request
        nrfClient.run();
    }

    private NrfConfig getNrfConfig() {
        NrfConfig nrfConfig = new NrfConfig();
        nrfConfig.setNrfDirectConnection(true);
        nrfConfig.setNrfAddress("127.0.0.1:8888");
        nrfConfig.setSnefNrfService(State.ENABLED);
        nrfConfig.setSmpIp("127.0.0.1");
        nrfConfig.setSmpPort("8080");
        return nrfConfig;
    }
}

Looks like I miss some server configuration, or use it in wrong way.

Or, maybe the reason is in powermock: could it be that mockserver is incompatible with powermock or PowerMockRunner?

0

There are 0 answers