我需要使用 JUnit 测试用例在 Eclipse 中测试我的 REST Web 服务。我该如何继续?是否与普通的Java类测试用例类似。
服务:
@Path("/regionService")
public class RegionService {
@GET
@Path("getRegions")
@Produces(MediaType.APPLICATION_JSON)
public String getRegions(@QueryParam("parentTenancyID") int parentTenancyID) throws CustomFault{
String result = null;
RegionServiceImpl implObj = new RegionServiceImpl();
if(parentTenancyID!=0){
try{
result = implObj.getRegionServiceDetails(parentTenancyID);
}catch(Exception e){
e.printStackTrace();
}
}
return result;
}
}
实现:
public class RegionServiceImpl {
@SuppressWarnings({ "rawtypes", "unchecked" })
public String getRegionServiceDetails(int parentTenancyID) {
String result = null;
Logger fLogger = FatalLoggerClass.logger;
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String regionQ = "select a.tenancy_id, a.tenancy_name from TenancyEntity a where Parent_Tenancy_ID=" +parentTenancyID+" ";
Query query = session.createQuery(regionQ);
try{
Iterator iterator = query.list().iterator();
Object[] resultQ = null;
HashMap<Integer,String> regionMap = new HashMap<Integer,String>();
JSONObject jsonObj = null;
JSONArray jsonArray = new JSONArray();
jsonObj = new JSONObject();
while(iterator.hasNext()){
resultQ = (Object[]) iterator.next();
int tenancyId = (Integer) resultQ[0];
String TenancyName = (String) resultQ[1];
regionMap.put(tenancyId, TenancyName);
}
jsonObj.putAll(regionMap);
jsonArray.add(jsonObj);
result = jsonArray.toString();
}catch(Exception e){
fLogger.fatal("Exception :"+e);
}finally{
if(session.getTransaction().isActive()){
session.getTransaction().commit();
}if(session.isOpen()){
session.flush();
session.close();
}
}
return result;
}
}
我想检查服务的 JSON 响应是否等于预期值。
我是一名优秀的程序员,十分优秀!