我是Spring MVC的新手,我使用spring MVC和resteasy编写了web服务。我的控制器工作正常,现在需要写testcase但我尝试了writtig但我从未接受过我也在自动装配中遇到问题。
@Controller
@Path("/searchapi") public class SearchAPIController implements ISearchAPIController { @Autowired private ISearchAPIService srchapiservice; @GET @Path("/{domain}/{group}/search") @Produces({"application/xml", "application/json"}) public Collections getSolrData( @PathParam("domain") final String domain, @PathParam("group") final String group, @Context final UriInfo uriinfo) throws Exception { System.out.println("LANDED IN get****************"); return srchapiservice.getData(domain, group, uriinfo); } }
任何人都可以在spring mvc中为我提供测试用例的示例代码。
答案 0 :(得分:4)
“Spring-MVC”测试用例看起来像使用模拟对象,例如我们想测试我的MyControllerToBeTest:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring.xml")
public class MyControllerTest {
private MockHttpServletRequest request;
private MockHttpServletResponse response;
private MyControllerToBeTested controller;
private AnnotationMethodHandlerAdapter adapter;
@Autowired
private ApplicationContext applicationContext;
@Before
public void setUp() {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
response.setOutputStreamAccessAllowed(true);
controller = new MyControllerToBeTested();
adapter = new AnnotationMethodHandlerAdapter();
}
@Test
public void findRelatedVideosTest() throws Exception {
request.setRequestURI("/mypath");
request.setMethod("GET");
request.addParameter("myParam", "myValue");
adapter.handle(request, response, controller);
System.out.println(response.getContentAsString());
}
}
但是我没有任何REST资源测试经验,在你的情况下是RestEasy。
答案 1 :(得分:0)
如果要测试容器内的完整服务,可以查看Java的REST Assured框架。它使测试和验证基于HTTP / REST的服务变得非常容易。