Dropwizard应用程序集成测试,DropwizardAppRule端口为空

时间:2019-06-07 15:41:27

标签: dropwizard

我对Dropwzard相当陌生,因此如果我做的不对,请随时指导我。这是我的情况。

我正在按照一本书的教程编写针对整个应用程序的集成测试(运行正常)。但是,我的测试运行,但没有在指定端口上运行完整的应用程序。以下是我的测试

 import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;

import demo.basicapi.configuration.BasicConfiguration;
import demo.basicapi.domain.Contact;
import io.dropwizard.testing.junit.DropwizardAppRule;
import junit.framework.TestCase;

public class AppTest extends TestCase {

    private Client client;
    private Contact dummyClient = new Contact(0, "Jane", "Doe", "+987654321");

    @ClassRule
    public static final DropwizardAppRule<BasicConfiguration> RULE = new DropwizardAppRule<BasicConfiguration>(
            App.class, "config.yaml");

    @Before
    public void setUp() {
        client = ClientBuilder.newBuilder().build();
        // Set the credentials to be used by the client
        HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("admin", "password");
        client.register(feature);
    }

    @Test
    public void testcreateAndRetrieveContact() {
        // Create a new contact by performing the appropriate http request (POST)

        WebTarget target = client.target(String.format("http://localhost:%d/contact", RULE.getLocalPort()));
        Response response = target.request(MediaType.APPLICATION_JSON)
                .post(Entity.entity(dummyClient, MediaType.APPLICATION_JSON));

        // Check that the response has the appropriate response code (201)
        assertEquals(response.getStatus(), 201);

    }

}

但是,当我运行测试时,检索端口号时会出现Null指针异常

RULE.getLocalPort() // This returns null in my test.

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.274 sec <<< FAILURE!
testcreateAndRetrieveContact(demo.basicapi.AppTest)  Time elapsed: 0.132 sec  <<< ERROR!
java.lang.NullPointerException
    at java.base/java.util.Objects.requireNonNull(Objects.java:221)
    at io.dropwizard.testing.DropwizardTestSupport.getLocalPort(DropwizardTestSupport.java:249)
    at io.dropwizard.testing.junit.DropwizardAppRule.getLocalPort(DropwizardAppRule.java:180)
    at demo.basicapi.AppTest.testcreateAndRetrieveContact(AppTest.java:41)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

另外,我有点担心,如果我玩这行,无论如何都不会出现任何错误,这与配置文件的名称无关,

@ClassRule
    public static final DropwizardAppRule<BasicConfiguration> RULE = new DropwizardAppRule<BasicConfiguration>(
            App.class, "config.yaml");

将.yaml文件的名称更改为不存在的名称不会在测试中引发任何其他错误。

我正在使用Dropwizard 1.3.12版,是否有任何我可以更改以使其成功的内容。我暂时不关注单独的表示法测试,但可以随时提出建议。谢谢。

<dependency>
            <groupId>io.dropwizard</groupId>
            <artifactId>dropwizard-testing</artifactId>
            <version>1.3.12</version>
        </dependency>

        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>

0 个答案:

没有答案