Forráskód Böngészése

修改自动化测试Controller异常的返回状态码以及相关测试

LiHaoyu 5 éve
szülő
commit
4928d88a78

+ 14 - 0
src/main/java/net/mooctest/www/android_auto_test/common/exceptions/LackParamsException.java

@@ -0,0 +1,14 @@
+package net.mooctest.www.android_auto_test.common.exceptions;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+/**
+ * @author henrylee
+ */
+@ResponseStatus(code = HttpStatus.BAD_REQUEST)
+public class LackParamsException extends Exception{
+    public LackParamsException(String msg){
+        super(msg);
+    }
+}

+ 3 - 2
src/main/java/net/mooctest/www/android_auto_test/controller/AutoTestController.java

@@ -1,5 +1,6 @@
 package net.mooctest.www.android_auto_test.controller;
 
+import net.mooctest.www.android_auto_test.common.exceptions.LackParamsException;
 import net.mooctest.www.android_auto_test.vo.TraceMetaInfo;
 import net.mooctest.www.android_auto_test.vo.TraceStatusResult;
 import net.mooctest.www.android_auto_test.services.AutoTestService;
@@ -21,7 +22,7 @@ public class AutoTestController {
     AutoTestService autoTestService;
 
     @RequestMapping(value = "/api/v1/runTest", method = RequestMethod.POST)
-    public TraceStatusResult runTest(@RequestBody TraceMetaInfo traceInfo){
+    public TraceStatusResult runTest(@RequestBody TraceMetaInfo traceInfo) throws LackParamsException {
         List<String> leakParams = new ArrayList<>();
         if (traceInfo.getTraceId() == null){
             leakParams.add("traceId");
@@ -30,7 +31,7 @@ public class AutoTestController {
             leakParams.add("downloadUrl");
         }
         if (leakParams.size() != 0){
-            throw new RuntimeException("缺少参数:" + Arrays.toString(leakParams.toArray()));
+            throw new LackParamsException("缺少参数:" + Arrays.toString(leakParams.toArray()));
         }
         return autoTestService.executeAutoTestTask(traceInfo);
     }

+ 1 - 1
src/test/java/net/mooctest/www/android_auto_test/ctrl/AutoTestControllerTest.java

@@ -83,7 +83,7 @@ public class AutoTestControllerTest {
             MvcResult result = mockMvc.perform(post("/api/v1/runTest")
                     .contentType(MediaType.APPLICATION_JSON)
                     .content(object.toString()))
-                    .andDo(print()).andExpect(status().is5xxServerError()).andReturn();
+                    .andDo(print()).andExpect(status().is4xxClientError()).andReturn();
         }catch (NestedServletException e){
             // assert
             Assert.assertEquals("缺少参数:[downloadUrl]", e.getRootCause().getMessage());

+ 2 - 2
src/test/java/net/mooctest/www/android_auto_test/ctrl/SecondaryControllerTest.java

@@ -46,7 +46,7 @@ public class SecondaryControllerTest {
     }
 
     @Test
-    public void testStart() throws Exception{
+    public void testStartWithParams() throws Exception{
         //arrange
         doNothing().when(secondaryService).generateSecondaryReport(traceId, toolName);
         //action
@@ -79,7 +79,7 @@ public class SecondaryControllerTest {
     }
 
     @Test
-    public void testStatus() throws Exception{
+    public void testStatusWithParams() throws Exception{
         //arrange
         SecondaryResult sr = new SecondaryResult();
         sr.setTraceId(traceId);