2017-10-07 89 views
0

我有一個簡單的Java Spring REST API應用程序,我不知道如何單元測試它。我已閱讀JUnit和Mockito的文檔,但我無法弄清楚。在Java和Spring中測試REST API的單元

這裏是在StudentController類

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) 
    public void insertStudent(@RequestBody Student student){ 
     studentService.insertStudent(student); 
    } 

POST方法在這裏是在StudentService類

public void insertStudent(Student student) { 
     studentDao.insertStudent(student); 
    } 

我使用MySQL數據庫insertStudent方法。我是否也應該使用數據庫進行單元測試?我的意思是我不想要任何集成測試。我只想單元測試。我在Node.js中使用supertest,它會照顧所有人,我可以使用JUnit或Mockito嗎?

+1

控制器的模擬單元測試會嘲笑服務並確保調用正確的方法,您當然可以使用JUnit + Mockito進行調用。那麼不需要數據庫,因爲真正的服務沒有被使用。 – jonrsharpe

+0

@jonrsharpe你能給我一個鏈接,顯示我可以做到這一點嗎? –

+0

已經有大量的例子,我會推薦研究。見例如https://stackoverflow.com/q/10906945/3001761 – jonrsharpe

回答

1

如果你想做單元測試,那麼你不必連接到數據庫。連接到DB和其他外部服務將被視爲集成測試。因此,在測試您的課程時,對數據庫的請求將被嘲笑。

第二點值得一提的是,你會分別測試你的控制器類和你的服務類,但在你的情況下,這些測試看起來非常相似。

下面是測試控制器的方法insertStrundent的一種方法。

@RunWith(MockitoJUnitRunner.class) 
public class TestStudentContoller { 

    @Mock 
    StundentService mockStudentService; 
    @InjectMocks 
    StudentController studentController = new StudentController(); 

    @Test 
    public void testInsertStudent(){ 

     Student student = new Student(); 

     studentContoller.insertStudent(student); 

     verify(studentService, times(1)).insertStudent(student); 
    } 

既然你控制器的insertStudent方法沒有if語句只有一個分支,基本上只有一個需要進行測試,基本上沒有控制器的呼叫服務。

另一種可以測試的方法是使用Springs MockMvc。關於MockMvc的好處是它可以讓你測試HTTP請求。例如,在這種情況下,您需要測試您的控制器的insertStudent方法是否會使用JSON學生正確響應HTTP POST請求。

@RunWith(MockitoJUnitRunner.class) 
public class TestStudentContoller { 

    @Mock 
    StundentService mockStudentService; 
    @InjectMocks 
    StudentController studentController = new StudentController(); 

    MockMvc mockMvc; 

    @Before 
    public void setup(){ 
     mockMvc = MockMvcBuilders.standAloneSetup(studentController).build(); 
    } 

    @Test 
    public void testInsertStudent(){ 

     Student student = new Student(); 

     studentContoller.insertStudent(student); 

     mockMvc.perform(post("path/to/insert/student") 
      .accept(MediaType.APPLICATION_JSON) 
      .andExpect(status().isOk()) 
      .andExpect(content().string("{}"));//put json student in here 

     verify(studentService, times(1)).insertStudent(student); 
    } 

MockMvc有你應該探索其他很酷的方法。

+0

Thanx Jose,這工作正常。 –

0

你想嘲笑studentService,並有一個單元測試,驗證,當API的方法insertStudent(Student)被稱爲然後恰好有一個調用該服務的insertStudent(Student)具有相同Student實例。

然後爲不同的場景單元測試,即處理null小號等。

1

我有一個簡單的Java春REST API應用

你真的應該更早開始思考單元測試。執行這些操作的最佳方法是在之前生產代碼執行(新)行爲(TDD)。

這裏是在StudentController類

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) 
    public void insertStudent(@RequestBody Student student){ 
     studentService.insertStudent(student); 
    } 

此代碼是過於簡單,無法 post方法。編寫這樣的代碼的單元測試是時間的腰線。這樣的代碼是通過應用測試模塊測試

我會盡快開始這個代碼編寫單元測試因爲有一些做出的決定(例如:根據輸入參數到其他對象的其他電話)。

的這裏的一點是,單元測試沒有測試碼 - 單元測試驗證期望的行爲(這是在您的要求表示)。所以是的:不測試這種方法會降低報告的覆蓋率。但任何覆蓋率工具計算的數量都不如要求覆蓋範圍,這些工具無法計算,您只能通過TDD來保證。