hi, all,
I am using dropwizard 1.0.5 to develop a RESTful service. When I unit test
a Resource, I get the following error messages:
Running io.datacase.meta.resources.ProjectResourceTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.828 sec
<<< FAILURE! - in io.datacase.meta.resources.ProjectResourceTest
io.datacase.meta.resources.ProjectResourceTest Time elapsed: 1.828 sec
<<< ERROR!
org.glassfish.jersey.server.model.ModelValidationException:
Validation of the application resource model has failed during application
initialization.
[[FATAL] No injection source found for a parameter of type public
javax.ws.rs.core.Response
io.datacase.meta.resources.ProjectResourceWithoutAuth.updateProject(io.datacase.meta.auth.Requester,io.datacase.meta.core.Project)
at index 0.; source='ResourceMethod{httpMethod=PUT,
consumedTypes=[application/json], producedTypes=[application/json],
suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS,
invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class
io.datacase.meta.resources.ProjectResourceWithoutAuth,
handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@f9b5552]},
definitionMethod=public javax.ws.rs.core.Response
io.datacase.meta.resources.ProjectResourceWithoutAuth.updateProject(io.datacase.meta.auth.Requester,io.datacase.meta.core.Project),
parameters=[Parameter [type=class io.datacase.meta.auth.Requester,
source=null, defaultValue=null], Parameter [type=class
io.datacase.meta.core.Project, source=null, defaultValue=null]],
responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']
Results :
Tests in error:
ProjectResourceTest.io.datacase.meta.resources.ProjectResourceTest »
ModelValidation
The source snippets shows below:
*ProjectResource.java:*
@Path("/projects/{id}")
@Produces(MediaType.APPLICATION_JSON)
public class ProjectResource {
...
@RolesAllowed(Roles.USER)
@GET
public Project findById(@Auth Requester requester, @PathParam("id")
LongParam id) {
return projectDAO.findById(id.get());
}
@RolesAllowed(Roles.USER)
@PUT
public Response updateProject(*@Auth Requester requester*, *@Valid
Project project*) {
int rows = projectDAO.update(project);
...
}
}
*ProjectResourceTest.java:*
public class ProjectResourceTest {
private static final ProjectDAO dao = mock(ProjectDAO.class);
@ClassRule
public static final ResourceTestRule resources =
ResourceTestRule.builder()
.addResource(new ProjectResource(dao, null, null, null, null,
null))
.build();
private final Project project = new Project(1);
@Before
public void setup() {
when(dao.findById(eq(1L))).thenReturn(project);
when(dao.findByName(eq("blah"))).thenReturn(project);
when(dao.update(project)).thenReturn(1);
}
@After
public void tearDown(){
reset(dao);
}
@Test
public void testGetProject() {
assertThat(resources.client().target("/projects/1").request().get(Project.class))
.isEqualTo(project);
verify(dao).findById(1L);
}
@Test
public void testUpdateProject() {
assertThat(resources.client().target("/projects/1").request().get()).isEqualTo(null);
}
}
The first test case testGetProject is all ok.
The second test case testUpdateProject:
1. If there is no @Auth parameter, it validates ok;
2. If there is the first parameter with @Auth annotation, and @BeanParam
with the second parameter, it validates ok.
3. If there is the first @Auth parameter, and no @*Param with the second
parameter, it validates failed.
Any ideas?
--
You received this message because you are subscribed to the Google Groups
"dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.