Skip to content

After mock tests request.get() always returns null when run junit tests with an embedded nginx-clojure  #101

Closed
@xfeep

Description

@xfeep

The original report of this issue is from nginx-clojure google group .
When we run both mock tests with mocked NginxJavaRequest and junit tests with an embedded nginx-clojure request.get() will always return null if we run junit tests with an embedded nginx-clojure after mock tests with mocked NginxJavaRequest. e.g.

TestByMock.java

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import nginx.clojure.MiniConstants;
import nginx.clojure.embed.NginxEmbedServer;
import nginx.clojure.java.ArrayMap;
import nginx.clojure.java.NginxJavaRequest;

public class TestByMock {

    static {
        Map<String, String> opts = ArrayMap.create("port", "0", 
                "http-user-defined", "handlers_lazy_init on;");
        NginxEmbedServer.getServer().start("empty-handler", opts);
        NginxEmbedServer.getServer().stop();
    }

    @Mock
    private NginxJavaRequest nginxJavaRequest;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks( this );
        when(nginxJavaRequest.get(MiniConstants.SCHEME)).thenReturn("http");
        when(nginxJavaRequest.get(MiniConstants.SERVER_PORT)).thenReturn("8082");
        when(nginxJavaRequest.get(MiniConstants.URI)).thenReturn("/test.jsp");
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testHandler() throws IOException {
        Object[] resp = new HelloURIHandler().invoke(nginxJavaRequest);
        assertEquals(3, resp.length);
        assertEquals(200, resp[0]);
        assertEquals("Hello, /test.jsp, 8082", resp[2]);
    }

}

TestByMock.java

import java.util.Map;
import static org.junit.Assert.assertEquals;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import nginx.clojure.embed.NginxEmbedServer;
import nginx.clojure.java.ArrayMap;

public class TestByEmbed {

    @Before
    public void setUp() throws Exception {
        Map<String, String> opts = ArrayMap.create("port", "8082");
        NginxEmbedServer.getServer().start(HelloURIHandler.class.getName(), opts);
    }

    @After
    public void tearDown() throws Exception {
        NginxEmbedServer.getServer().stop();
    }

    @Test
    public void testRemote() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet("http://127.0.0.1:8082/test.jsp");
        try (CloseableHttpResponse httpResponse = httpclient.execute(httpget)) {
            assertEquals("Hello, /test.jsp, 8082", EntityUtils.toString(httpResponse.getEntity()));
        }
    }

}

Result of running both of them

org.junit.ComparisonFailure: expected:<Hello, [/test.jsp, 8082]> but was:<Hello, [null, null]>
    at org.junit.Assert.assertEquals(Assert.java:115)
    at org.junit.Assert.assertEquals(Assert.java:144)
    at nginx.clojure.mock.TestByEmbed.testRemote(TestByEmbed.java:35)

It can be resolved by start + stop NginxEmbedServer before running tests. e.g.

public class TestByMock {

    static {
                // for v0.4.2 
        Map<String, String> opts = ArrayMap.create("port", "0", 
                "http-user-defined", "handlers_lazy_init on;");
                // for v0.4.1 
                // Map<String, String> opts = ArrayMap.create("port", no-zero-port, 
        //      "http-user-defined", "handlers_lazy_init on;");
        NginxEmbedServer.getServer().start("empty-handler", opts);
        NginxEmbedServer.getServer().stop();
                NginxJavaHandlerFactory.fetchFactory("java");
                //uncomment next line when use clojure
                //NginxJavaHandlerFactory.fetchFactory("clojure");
    }

It will be fixed in release v0.4.3 .

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions