javax.ws.rs.NotFoundException:找不到针对junit + jersy测试框架的HTTP 404

时间:2018-05-14 18:18:49

标签: java junit mockito javax jersey-test-framework

Junit test case :logControllerLuaTest.java
----------------------------

package Tests;


import static org.junit.Assert.*;
import Tests.serviceinfo2;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

import com.koopid.apex.API.rest.controller.LogControllerLua;

//import static com.jayway.restassured.RestAssured.expect;
//import static com.jayway.restassured.RestAssured.get;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import com.google.gson.Gson;
import com.koopid.apex.API.rest.service.LoggerServiceLua;




import static org.mockito.Mockito.*;


import java.util.Arrays;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import javax.ws.rs.core.Application;

import java.util.Arrays;
import java.util.List;


public class LogControllerLuaTest extends JerseyTest {


    @Mock
    private LoggerServiceLua loggerServiceLua;

    @InjectMocks
    private LogControllerLua logControllerLua;

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



    @Before
    public void init(){

        System.out.println("in here 1");
        MockitoAnnotations.initMocks(this);     
        System.out.println("in here 1 end");
        }


        @Test
        public void testgetLogServerInfo() throws Exception
        {
            System.out.println("executing in test");
            String serviceinfo = new String("some data");

            //serviceinfo2 serviceinfo2 = mock(serviceinfo2.class);
            when(loggerServiceLua.getLogServerInfo()).thenReturn(serviceinfo);
            System.out.println("in test 1 end");
            String response = target("/API/rest/getLogServerInfo").request().get(String.class);
            System.out.println("in test 2 end");
            Assert.assertTrue(serviceinfo.equals(response));
            System.out.println("in test 3 end");
            System.out.println(serviceinfo);

        }
    }



JersyConfig.java :
--------------------
package Tests;


import org.glassfish.jersey.server.ResourceConfig;

import com.koopid.apex.API.rest.controller.*;
import com.koopid.apex.API.rest.service.LoggerServiceLua;


public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        register(LogControllerLua.class);
        register(LoggerServiceLua.class);
    }

}

如果我在configure()中没有使用JerseyConfig.class,它说没有找到注入文件。

javax.ws.rs.NotFoundException: HTTP 404 Not Found
    at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1008)
    at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:816)
    at org.glassfish.jersey.client.JerseyInvocation.access$700(JerseyInvocation.java:92)
    at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:700)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:696)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:420)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:316)
    at Tests.LogControllerLuaTest.testgetLogServerInfo(LogControllerLuaTest.java:92)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

我正在编写单元测试用例来测试rest api并点击get请求,我正在嘲笑服务

Dim xlApp As New Excel.Application
Sub test()
   Range("D1") = xlApp.WorksheetFunction.RTD("tickerplantrtdserver", Null, "4#2#1#6768#FUTSTK#N1#0#XX#Bid")
End Sub

请帮助我解决这个问题?如果需要任何其他信息,请提前感谢。请注意,有人可以解释为什么我们需要覆盖configure()方法吗?

1 个答案:

答案 0 :(得分:0)

通过进行1次更改解决了上述问题 1.通过在受保护的应用程序configure(){中初始化mockitoAnnotation MockitoAnnotations.initMocks(本);

并注册控制器和服务类 1. @Override     protected Application configure(){

    MockitoAnnotations.initMocks(this);
    ResourceConfig resourceConfig = new ResourceConfig();
    resourceConfig.register(MultiPartFeature.class);
    resourceConfig.register(DBRestClient.class);
    resourceConfig.register(LoggerServiceLua.class);
    resourceConfig.register(LogControllerLua.class);

return resourceConfig; }