Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring Jersey应用程序的Jersey测试中资源模型验证失败_Spring_Jersey_Jersey 2.0_Jersey Test Framework - Fatal编程技术网

Spring Jersey应用程序的Jersey测试中资源模型验证失败

Spring Jersey应用程序的Jersey测试中资源模型验证失败,spring,jersey,jersey-2.0,jersey-test-framework,Spring,Jersey,Jersey 2.0,Jersey Test Framework,我有一个带注释的Spring Jersey应用程序。我正在尝试使用JerseyTest为我的控制器设置单元测试。我在运行测试时遇到了以下错误,我无法找出这些错误。我错过了什么 SEVERE: Following issues have been detected: WARNING: No injection source found for a parameter of type public com.example.services.dto.UnitDto com.example.apis.

我有一个带注释的Spring Jersey应用程序。我正在尝试使用
JerseyTest
为我的控制器设置单元测试。我在运行测试时遇到了以下错误,我无法找出这些错误。我错过了什么

SEVERE: Following issues have been detected: 
WARNING: No injection source found for a parameter of type public com.example.services.dto.UnitDto com.example.apis.UnitResource.getUnits(com.example.services.dto.PointDto) throws com.example.exceptions.PointOutsideBounds at index 2.

Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public com.example.services.dto.UnitDto com.example.apis.UnitResource.getUnits(com.example.services.dto.PointDto) throws com.example.exceptions.PointOutsideBounds at index 2.; source='ResourceMethod{httpMethod=GET, consumedTypes=[], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.example.apis.UnitResource, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@31a5870e]}, definitionMethod=public com.example.services.dto.UnitDto com.example.apis.UnitResource.getUnits(com.example.services.dto.PointDto) throws com.example.exceptions.PointOutsideBounds, parameters=[Parameter [type=class com.example.services.dto.PointDto, source=contains, defaultValue=null]], responseType=class com.example.services.dto.UnitDto}, nameBindings=[]}']
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:555)
控制器类如下所示:

@Component
@Path("/app/units")
public class UnitResource {
    @Context
    private UriInfo uriInfo;
    @Context
    private Request request;

    @Inject
    private UnitService unitService;

    @Inject
    public UnitResource(@Named("UnitService") UnitService unitService) {
        this.unitService = unitService;
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Timed(absolute = true, name = "getUnitForPoint")
    public UnitDto getUnits(@QueryParam("contains") @NotNull PointDto point) throws PointOutsideBounds {
        return unitService.getUnitForPoint(point);
    }
}
public class UnitResourceTest extends JerseyTest {

    @Mock
    private UnitService unitService;

    @Override
    protected ResourceConfig configure() {
        ResourceConfig rc = new ResourceConfig()
        //      .register(new UnitResource(Mockito.mock(UnitService.class))) -- has the same effect
                .register(UnitResource.class)
                .property("contextConfig", new AnnotationConfigApplicationContext(ApplicationConfiguration.class));

        enable(TestProperties.LOG_TRAFFIC);
        enable(TestProperties.DUMP_ENTITY);
        forceSet(TestProperties.CONTAINER_PORT, "0");

        return rc;
    }

    @Before
    public void setUp() throws Exception {
        super.setUp();
        initMocks(this);
    }

    @Override
    protected TestContainerFactory getTestContainerFactory() {
        return new InMemoryTestContainerFactory();
    }

    @Test
    public void testGetUnit() throws PointOutsideBounds {

        Response response = target()
                .path("/app/units")
                .queryParam("contains", new Point(0.0,0.0).toJson())
                .request(MediaType.APPLICATION_JSON_TYPE)
                .get();

        assertThat(response.getStatus(), is(Response.Status.OK));
    }
}
测试等级如下:

@Component
@Path("/app/units")
public class UnitResource {
    @Context
    private UriInfo uriInfo;
    @Context
    private Request request;

    @Inject
    private UnitService unitService;

    @Inject
    public UnitResource(@Named("UnitService") UnitService unitService) {
        this.unitService = unitService;
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Timed(absolute = true, name = "getUnitForPoint")
    public UnitDto getUnits(@QueryParam("contains") @NotNull PointDto point) throws PointOutsideBounds {
        return unitService.getUnitForPoint(point);
    }
}
public class UnitResourceTest extends JerseyTest {

    @Mock
    private UnitService unitService;

    @Override
    protected ResourceConfig configure() {
        ResourceConfig rc = new ResourceConfig()
        //      .register(new UnitResource(Mockito.mock(UnitService.class))) -- has the same effect
                .register(UnitResource.class)
                .property("contextConfig", new AnnotationConfigApplicationContext(ApplicationConfiguration.class));

        enable(TestProperties.LOG_TRAFFIC);
        enable(TestProperties.DUMP_ENTITY);
        forceSet(TestProperties.CONTAINER_PORT, "0");

        return rc;
    }

    @Before
    public void setUp() throws Exception {
        super.setUp();
        initMocks(this);
    }

    @Override
    protected TestContainerFactory getTestContainerFactory() {
        return new InMemoryTestContainerFactory();
    }

    @Test
    public void testGetUnit() throws PointOutsideBounds {

        Response response = target()
                .path("/app/units")
                .queryParam("contains", new Point(0.0,0.0).toJson())
                .request(MediaType.APPLICATION_JSON_TYPE)
                .get();

        assertThat(response.getStatus(), is(Response.Status.OK));
    }
}
应用程序配置类为:

@Configuration
@ComponentScan("com.example")
public class ApplicationConfiguration {
    @Bean(name="UnitService")
    public UnitService unitService() {
        return new UnitService();
    }
}
我的测试配置的渐变依赖项是:

dependencies {
    // Using junit-dep package to get junit without hamcrest dependency
    testCompile("junit:junit-dep:4.8.2")
    testCompile("org.hamcrest:hamcrest-all:1.3")
    testCompile("org.mockito:mockito-all:1.8.4")
    testCompile ('org.glassfish.jersey.test-framework:jersey-test-framework-core:2.22.2')
    testCompile('org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-inmemory:2.22.2')
}

configurations {
    testCompile.exclude group: 'org.glassfish.jersey.ext', module: 'jersey-spring3'
}

我找到了,但都没有解决我的问题。

注意:在大多数情况下,这个答案适用于所有带注释的参数

指向
参数的点似乎是问题所在

public UnitDto getUnits(@QueryParam("contains") @NotNull PointDto point)
查询参数是字符串。Jersey能够将该字符串转换为一些基本类型,如
int
boolean
Date
,等等。但它不知道如何从该字符串创建指向
点。这并不意味着我们不能使用自定义类型。我们只需要遵循一些规则:

  • 具有接受字符串的构造函数。然后我们需要自己从这个字符串构造对象。比如说

    public PointDto(String param) {
        Map<String, Object> map = parseJson(param);
        this.field1 = map.get("field1");
        this.field2 = map.get("field2");
    }
    
  • 您可以使用
    参数转换器提供程序
    。我不会举一个例子,但您可以阅读更多关于如何实现它的内容。你也可以看到


  • 请注意,此答案中提供的信息位于的javadoc中。

    谢谢。完全没有注意到这一点。我正在重新考虑将JSON作为查询参数传递。我将发送一个逗号分隔的字符串,如
    x,y,z
    ,并在控制器内部初始化
    PointDto
    。我猜这是因为当Jersey试图查找SpringXML上下文文件时,您从Jersey获得了filenotfoundexception。您可以查看中的配置,了解如何使用注释配置。这样,您就可以在测试中使用不同的Spring配置文件,而不必对所有内容都使用mock。我想我会提出来的。谢谢,这很有帮助。我现在正在编写单元测试,所以我确实需要模拟一切。但我会在开发集成测试时使用它。实际上,不用担心。再看看你的代码,你已经这样做了:-)我不是一个Gradle用户,所以我可能误解了
    testCompile.exclude
    的意思