title: JUnit Test Cases for Web Services
body: <p>I need to test my REST Web Service in Eclipse using JUnit test cases. How can I proceed? Is it similar to normal Java classes test cases.</p>
  
  <p>Services:</p>
  
  <pre><code>@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;
     }
  }
  </code></pre>
  
  <p>Implementation:</p>
  
  <pre><code>    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&lt;Integer,String&gt; regionMap = new HashMap&lt;Integer,String&gt;();
          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;
  }
  }
  </code></pre>
  
  <p>I want to check whether the JSON response from the service will be equal to the expected value.</p>