使用SpringJunitRunner的测试用例

时间:2015-06-17 14:31:32

标签: spring junit jersey

我想用springJunitRunner为下面的代码编写junit测试用例。 下面的代码是一个类中的一个服务。

<tbody>
        <tr ng-repeat="plan in scheduler.plans | orderBy:'standardTime'">
            <td class="plan-time">{{plan.displayTime}}</td>
            <td class="plan-duration">{{plan.duration}} minutes</td>
            <td class="plan-task">{{plan.task}}</td>
            <td><button type="button" ng-class="{'btn-primary': !plan.clicked, 'btn-success': plan.clicked}" ng-click="plan.clicked = !plan.clicked">Yes</button></td>
       </tr>
    </tbody>

servlet的web.xml中的配置是

@Component
@Path(/techStack)
public class TechStackResource {



@Autowired
  private transient TechStackService techStackService;

@GET
 @Path("/{id}")
 @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  public Response getTechStackById(final @PathParam("id") Integer technicalstackid) {
    final TechStackResponse response = new TechStackResponse();
    int statusCode = Constants.HTTP_STATUS_OK_200;
    try {
      TechStackModel techStackModel = techStackService.findObjectById(technicalstackid);
      response.setGetTechStackDetails(GetTechStackDetails.newBuilder().technicalStack(techStackModel).build());

      if (techStackModel == null) {
        statusCode = Constants.HTTP_STATUS_ERROR_404;
      }

    } catch (EmptyResultDataAccessException erde) {

    } catch (Exception e) {
      LOGGER.error("Exception occured in TechStackResource.getTechStackById(technicalstackid) ", e);
      throw new APMRestException(
          "Exception while executing TechStackResource.getTechStackById(technicalstackid) ",
          Constants.UNKNOW_ERROR, e);
    }
    return Response.status(statusCode).entity(response).build();
  }
}

1 个答案:

答案 0 :(得分:0)

由于您使用 Jersey 以及 Spring ,因此您只能使用SpringJunitRunner连接TechStackResource及其依赖关系{ {1}}。

为了测试您的REST处理程序方法TechStackService,您可以使用POJO方法并直接调用它。或者,您可以使用Jersey自己的MockWeb环境。要了解更多相关信息,我建议您查看Jersey示例来源,例如: HelloWorld