Spring Restful Api 单元测试

2017-07-05  本文已影响0人  無式

和任何做产品的公司一样,软件公司对其软件产品的质量也是十分看重。
虽然任何公司都会有测试部门,但软件产品的质量不能完全靠QA同学们的加班来保障。软件质量从程序开发阶段就应该引起重视。
单元测试便是在开发阶段中保证质量的一种重要方式。

什么是单元测试

单元测试的目的

Spring Restful Api 服务单元测试

就以现在流行的Restful服务来举一个单元测试例子,主要包含ControllerService层。

测试工具,使用junit+mockito+powermock测试框架

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.8.4</version>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.5.6</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.5.6</version>
    <scope>test</scope>
</dependency>

创建单元测试基类

@ContextConfiguration(classes = {WebConfig.class })
@RunWith(SpringJUnit4ClassRunner.class)  
public class BaseCtlCfg extends AbstractContextControllerTests  {
}

Controller层单元测试

public class DraftControllerTest extends BaseCtlCfg{
    @Mock
    private  DraftService draftService;
    @Autowired
    protected WebApplicationContext wac;
    @Before
    public void before(){
        //初始化Mock
        MockitoAnnotations.initMocks(this);
        //获取上下文容器中的controller
        DraftController draftController = (DraftController) wac.getBean("draftController");
        //用mock的Service替换原有的service
        ReflectionTestUtils.setField(draftController, "draftService", draftService);
        }
    @Test
    public void publishTest() throws Exception {
        DraftPublishReq request = new DraftPublishReq();
        request.setPublishMode(1);
        request.setClassIds("495331469627");
        request.setPublishTime(System.currentTimeMillis());
        request.setPublishTime(System.currentTimeMillis() + 2*24*60*60*1000);

        String json = JSON.toJSONString(request);
        String uri = "/v0.3/drafts/00691d21-71f4-4115-89d0-a7168c77ef8b/actions/publish";

        //构建模拟数据。
        DraftInfo draftInfo = new DraftInfo();
        draftInfo.setIsdel(false);
        //模拟方法调用,当draftId=00691d21-71f4-4115-89d0-a7168c77ef8b时,return draftInfo对象
        when(draftService.get("00691d21-71f4-4115-89d0-a7168c77ef8b")).thenReturn(draftInfo);

        String resStr = mockMvc.perform(post(uri, "json")
                .characterEncoding("UTF-8")
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.valueOf(MockUtil.APPLICATION_JSON))
                .content(json.getBytes()))
                .andExpect(status().isOk())
                .andReturn().getResponse().getContentAsString();

        JSONArray jsonArray = JSON.parseArray(resStr);
        Assert.assertEquals(1, jsonArray.size());
    }
}

Service层测试

@RunWith(PowerMockRunner.class)
@PrepareForTest(LifecycleServiceV06.class)
public class HomeworkBizServiceTest{

    @Spy //mock真实对象
    private HomeworkBizService homeworkBizService = new HomeworkBizService();
    @Mock
    private HomeworkExtendRepository homeworkExtendRepository;

    @Before
    public void before() {
        //初始化Mock
        MockitoAnnotations.initMocks(this);
        //用mock的repository替换原有的repository
        ReflectionTestUtils.setField(homeworkBizService, "homeworkExtendRepository", homeworkExtendRepository);
    }

    @Test
    public void createHomeworkPackage() throws Exception {
        Long userId = 2107222934l;
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(String.valueOf(userId));
        String homeworkId="8b310d5d-ebd2-4798-bff3-2863fe1e1cd7";
        //构建模拟数据
         HomeworkExtend extend = new HomeworkExtend();
        extend.setLcmsHomeworkId(homeworkId);
        LifecycleSession session = new LifecycleSession();
        //mock类静态方法
         PowerMockito.mockStatic(LifecycleServiceV06.class);
        //模拟方法调用返回
         PowerMockito.when(LifecycleServiceV06.uploadHomework(homeworkId, userId)).thenReturn(session);
        //模拟方法调用返回
         when(homeworkExtendRepository.get(homeworkId)).thenReturn(extend);
        //void方法do nothing
        doNothing().when(homeworkBizService).createHomeworkPackage(homeworkId,session);

        homeworkBizService.createHomeworkPackage(homeworkId, userInfo);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读