2015-04-01 134 views
1

我想創建一個使用JUnit登錄頁面的測試,我需要模擬dev控制器。JUnit Mockito請求和響應

正如我在我的開發環境使用的HttpServlet,測試環境是要求的HttpRequest ...

我去模擬請求,其中我的文件沒有得到,我正在使用一個控制器不是一個servlet 。

任何人都可以幫助我嗎?

下面是我的JUnit控制器

package com.atoc.test.controller; 

import java.io.PrintWriter; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.mockito.InjectMocks; 
import org.mockito.Mock; 
import org.mockito.Mockito; 
import org.mockito.MockitoAnnotations; 
import org.springframework.mock.web.MockHttpServletRequest; 
import org.springframework.mock.web.MockHttpServletResponse; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import org.springframework.test.context.web.WebAppConfiguration; 
import org.springframework.test.web.servlet.MockMvc; 
import org.springframework.test.web.servlet.setup.MockMvcBuilders; 
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter; 

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; 
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; 
import static org.junit.Assert.assertNotNull; 
import static org.mockito.Mockito.when; 

import com.atoc.test.DAO.UserTestDAO; 
import com.atoc.test.service.UserTestService; 
import com.bpa.qaproduct.controller.UserController; 
import com.bpa.qaproduct.entity.User; 
import com.bpa.qaproduct.service.UserService; 

import org.apache.commons.io.FileUtils; 



@RunWith(SpringJUnit4ClassRunner.class) 
@WebAppConfiguration 
@ContextConfiguration(locations = { "classpath:testApplicationContext.xml" }) 
public class UserControllerTest{ 


    @Mock 
    private UserService userService; 

    @Mock 
    private HttpServletRequest request; 

    @Mock 
    private HttpServletResponse response; 

    @InjectMocks 
    private UserController userController; 

    private MockMvc mockMvc; 

    @Before 
    public void setup() { 
     MockitoAnnotations.initMocks(this); 

     mockMvc = MockMvcBuilders.standaloneSetup(userController).build(); 

    } 

    @Test 
    public void testviewUserrList() throws Exception { 
     User user = new User(); 

     //int value = null; 
     when(userService.getUserFilterCount(user)).thenReturn(20); 
     //mockMvc.perform(get("/todo/")); 
     mockMvc.perform(get("/user/viewUserList")).andExpect(status().isOk()); 
    } 

    @Test 
    public void testloginVerification() throws Exception 
    { 
     User user = new User(); 

     HttpServletRequest request = Mockito.mock(HttpServletRequest.class);  
     HttpServletResponse response = Mockito.mock(HttpServletResponse.class); 


     when(request.getParameter("userName")).thenReturn("me"); 
     when(request.getParameter("password")).thenReturn("secret"); 

     mockMvc.perform(post("/user/loginVerification")) 
     .andExpect(status().isOk()); 
     System.out.println("***********"+request.getParameter("userName")); 
    } 
} 

測試用例未通過而HttpRequest的不流通值。

所以我得到一個NullPointerException在我的運行環境

我嘲笑了請求和響應的方法,但我仍然得到同樣的錯誤。

傳遞值不爲空

我給一些值有 設定的時間可能是在那裏我使用的是內部方法

回答

1

你沒在主類擴展任何的的Mockito問題 必須提供請求參數給mockmvc而不是請求對象。

@Test 
public void testloginVerification() throws Exception { 
    User user = new User(); 

    mockMvc.perform(post("/user/loginVerification") 
     .param("userName", "me") 
     .param("password", "secret")) 
     .andExpect(status().isOk()); 
} 

另外User對象不與您測試的其他代碼進行交互。我認爲你在做一些完全錯誤的事情。你可以添加UserController的代碼嗎?

+0

謝謝你,我試圖以上,但仍空響應 – Dhivya 2015-04-02 05:24:00

+0

@RequestMapping(值= 「/用戶/ loginVerification」,方法= RequestMethod.POST) \t公共地圖@ResponseBody <字符串,對象> loginVerification(HttpServletRequest的請求) \t { (3);嘗試{用戶searchUser =新用戶();();}。 \t String userEmail = request.getParameter(「userEmail」); \t \t \t如果(USEREMAIL = NULL && userEmail.isEmpty()==假!){ \t \t \t \t searchUser.setUserEmail(USEREMAIL); \t \t \t} \t \t \t \t \t \t字符串的userPassword =用request.getParameter( 「的userPassword」); \t if(userPassword!= null && userPassword.isEmpty()== false)searchUser.setUserPassword(userPassword); } – Dhivya 2015-04-02 05:25:58

+0

以上是我的UserController.java – Dhivya 2015-04-02 05:27:21