Spring MVC:如何从返回String的控制器方法单元测试Model的属性?

时间:2016-10-19 02:16:17

标签: java spring unit-testing spring-mvc junit

例如,

Sub CopyThenPaste()

    Dim wb1 As Workbook
    Set wb1 = ActiveWorkbook

    Dim wsAtualizaABS As Worksheet
    Set wsAtualizaABS = wb1.Worksheets("AtualizaABS")

    Dim wb2 As Workbook

    Dim Destino As String
    Dim Dados As String
    Dim ABSid As String
    Dim Pasta As String

    On Error GoTo Errorcatch

    'States the number of the last row that contains relevant information to the Macro
    ultima_linha = wsAtualizaABS.Range("e2").End(xlDown).Row

    For i = 2 To ultima_linha
        Destino = wsAtualizaABS.Cells(i, 6).Value
        Dados = wsAtualizaABS.Cells(i, 7).Value
        ABSid = wsAtualizaABS.Cells(i, 5).Value

'********************
'**** This block of code can probably be executed outside the loop,
'**** unless the path to each workbook is different
        'Asks the user what is the folder where VBA should look for the Workbook with the new information
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Por favor escolha uma pasta"
            .AllowMultiSelect = False
            If .Show = -1 Then Pasta = .SelectedItems(1)
        End With
'********************

        'Opens the new workbook, copies and then pastes the data in the current Workbook
        Set wb2 = Workbooks.Open(Filename:=Pasta & "\" & ABSid & ".xls")
        wb2.Sheets(Dados).Cells.Copy Destination:=wb1.Worksheets(Destino).Range("A1")
        wb2.Close

    Next

    Exit Sub

Errorcatch:
    MsgBox Err.Description

End Sub

我想使用JUnit从package com.spring.app; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * Handles requests for the application home page. */ @Controller public class HomeController { @RequestMapping(value = "/", method = RequestMethod.GET) public String home(final Model model) { model.addAttribute("msg", "SUCCESS"); return "hello"; } } 测试model的属性及其值。我可以将返回类型更改为home()以使其成为可能,但我想使用ModelAndView因为它更简单。但这不是必须的。

无论如何检查String而不更改model的返回类型?或者它无法帮助?

3 个答案:

答案 0 :(得分:6)

您可以使用Spring MVC Test

mockMvc.perform(get("/"))
                .andExpect(status().isOk())
                .andExpect(model().attribute("msg", equalTo("SUCCESS"))) //or your condition

here是完全举例说明的例子

答案 1 :(得分:1)

我尝试使用副作用来回答这个问题。

@Test
public void testHome() throws Exception {
    final Model model = new ExtendedModelMap();
    assertThat(controller.home(model), is("hello"));
    assertThat((String) model.asMap().get("msg"), is("SUCCESS"));
}

但我对此仍然不是很有信心。如果这个答案有一些缺陷,请留下一些评论来改进/贬低这个答案。

答案 2 :(得分:0)

你可以使用Mockito。

示例:

@RunWith(MockitoJUnitRunner.class) 
public HomeControllerTest {

    private HomeController homeController;
    @Mock
    private Model model;

    @Before
    public void before(){
        homeController = new HomeController();
    }

    public void testSomething(){
        String returnValue = homeController.home(model);
        verify(model, times(1)).addAttribute("msg", "SUCCESS");
        assertEquals("hello", returnValue);
    }

}
相关问题