java jersey dto对象,用于Spring-Jersey应用程序的JerseyTest中的资源...

我有一个带注释的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));

}

}

应用程序配置类为:

@Configuration

@ComponentScan("com.example")

public class ApplicationConfiguration {

@Bean(name="UnitService")

public UnitService unitService() {

return new UnitService();

}

}

我对测试配置的Gradle依赖性是:

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'

}

我找到了this和this,但都没有解决我的问题.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值