Jersey测试在从主包运行方法时失败 -> 在target(…)处发生空指针异常。

huangapple 未分类评论44阅读模式
英文:

Jersey Test fails when running method from main package -> NullPointerException at target(...)

问题

这是我用这个示例进行的测试:

路径:pkg > src > test > java > rest > SimpleJerseyTest

public class SimpleJerseyTest extends JerseyTest {

    @Path("hello")
    public static class HelloResource {
        @GET
        public String getHello() {
            return "Hello World!";
        }
    }

    @Override
    protected Application configure() {
        return new ResourceConfig(HelloResource.class);
    }

    @Test
    public void test() {
        Response response = target("hello").request().get();

        assertEquals("Http Response should be 200: ", Response.Status.OK.getStatusCode(), response.getStatus());
        assertEquals("Http Content-Type should be: ", MediaType.TEXT_HTML, response.getHeaderString(HttpHeaders.CONTENT_TYPE));

        String content = response.readEntity(String.class);
        System.out.println("Gotten response: " + content);
        assertEquals("Content of ressponse is: ", "Hello World!", content);
    }
}

这是测试的输出:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Jul 24, 2020 11:29:07 AM org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory$GrizzlyTestContainer <init>
INFO: Creating GrizzlyTestContainer configured at the base URI http://localhost:9998/
Jul 24, 2020 11:29:08 AM org.glassfish.grizzly.http.server.NetworkListener start
INFO: Started listener bound to [localhost:9998]
Jul 24, 2020 11:29:08 AM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
Gotten response: Hello World!
Jul 24, 2020 11:29:08 AM org.glassfish.grizzly.http.server.NetworkListener shutdownNow
INFO: Stopped listener bound to [localhost:9998]

Process finished with exit code 0

但是,当我将 REST 服务类放在主包中时,它不再起作用(空指针异常):

包:pkg > src > main > java > rest > BookService

@Path("books")
public class BookService  {
    @GET
    public String getAll() {
        return "test";
    }
}

然后是测试:

包:pkg > src > test > rest > BookServiceTest

class BookServiceTest extends JerseyTest {
    @Override
    protected Application configure() {
        return new ResourceConfig(BookService.class);
    }

    @Test
    void get() {
        Response response = target("books").request().get();
    }
}

这是测试的输出:

java.lang.NullPointerException
	at org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:541)
	at org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:555)
	at rest.BookServiceTest.get(BookServiceTest.java:21)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

// ...

Process finished with exit code -1

有什么区别?有谁知道为什么使用静态内部类可以正常工作,但使用外部类就不行了吗?

英文:

So I have tested it with this example:

Path: pkg &gt; src &gt; test &gt; java &gt; rest &gt; SimpleJerseyTest

public class SimpleJerseyTest extends JerseyTest {

    @Path(&quot;hello&quot;)
    public static class HelloResource {
        @GET
        public String getHello() {
            return &quot;Hello World!&quot;;
        }
    }

    @Override
    protected Application configure() {
        return new ResourceConfig(HelloResource.class);
    }

    @Test
    public void test() {
        Response response = target(&quot;hello&quot;).request().get();

        assertEquals(&quot;Http Response should be 200: &quot;, Response.Status.OK.getStatusCode(), response.getStatus());
        assertEquals(&quot;Http Content-Type should be: &quot;, MediaType.TEXT_HTML, response.getHeaderString(HttpHeaders.CONTENT_TYPE));

        String content = response.readEntity(String.class);
        System.out.println(&quot;Gotten response: &quot; + content);
        assertEquals(&quot;Content of ressponse is: &quot;, &quot;Hello World!&quot;, content);
    }
}

This is the output of the test:

SLF4J: Failed to load class &quot;org.slf4j.impl.StaticLoggerBinder&quot;.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Jul 24, 2020 11:29:07 AM org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory$GrizzlyTestContainer &lt;init&gt;
INFO: Creating GrizzlyTestContainer configured at the base URI http://localhost:9998/
Jul 24, 2020 11:29:08 AM org.glassfish.grizzly.http.server.NetworkListener start
INFO: Started listener bound to [localhost:9998]
Jul 24, 2020 11:29:08 AM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
Gotten response: Hello World!
Jul 24, 2020 11:29:08 AM org.glassfish.grizzly.http.server.NetworkListener shutdownNow
INFO: Stopped listener bound to [localhost:9998]



Process finished with exit code 0

But when I put the REST service class at in the main package, it doesnt work anymore (NullPointerException):

Package: pkg &gt; src &gt; main &gt; java &gt; rest &gt; BookService

@Path(&quot;books&quot;)
public class BookService  {
    @GET
    public String getAll() {
        return &quot;test&quot;;
    }
}

And then the Test:

Package: pkg &gt; src &gt; test &gt; rest &gt; BookServiceTest

class BookServiceTest extends JerseyTest {
    @Override
    protected Application configure() {
        return new ResourceConfig(BookService.class);
    }

    @Test
    void get() {
        Response response = target(&quot;books&quot;).request().get();
    }
}

This is the output of the test:

java.lang.NullPointerException
	at org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:541)
	at org.glassfish.jersey.test.JerseyTest.target(JerseyTest.java:555)
	at rest.BookServiceTest.get(BookServiceTest.java:21)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

// ...



Process finished with exit code -1

What is the difference? Does anyone have a clue, why it works with a static inner class, but not with a external one?

答案1

得分: 0

弄清楚了 - 我使用了错误的导入。

应该使用import org.junit.Test,而不是import org.junit.jupiter.api.Test

英文:

Figured it out - I used the wrong import.

Instead of import org.junit.jupiter.api.Test it should be import org.junit.Test.

huangapple
  • 本文由 发表于 2020年7月24日 17:36:31
  • 转载请务必保留本文链接:https://java.coder-hub.com/63070896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定