用于控制器测试的模拟设备

时间:2017-05-15 13:30:29

标签: java unit-testing spring-mvc controller mocking

我正在尝试为控制器编写单元测试,根据设备类型执行不同的操作。

第一个控制器方法及其测试工作正常,方法参数 model 有一个值,我可以验证测试中的值。

第二种控制器方法,它要求设备在从brwoser调用时工作,但在单元测试中。当调用测试方法doSomething2Test()时,我得到了一个

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.mobile.device.Device]: Specified class is an interface

我的问题:为什么参数 model 正确实例化但设备不是?有没有办法在MockMvc请求中设置设备,还是可以以某种方式模拟设备?

控制器:

@Controller
@RequestMapping("home")
public class MyController {

    @RequestMapping("/test1")
    public String doSomething1(Model model) {
        String value = "foo";
        model.addAttribute("attribute1", value);        
        return "newUrl";
    }

    @RequestMapping("/test2")
    public String doSomething2(Model model, Device device) {
        if (device.isNormal()) {
          // do stuff
        }
        return "newUrl";
    }
}

测试:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT)
@WithMockUser(username="admin", authorities = {"ALL"})
public class MyControllerTest {

    private MockMvc mockMvc;

    @InjectMocks
    private MyController controllerUnderTest;   

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/classes/templates");
        viewResolver.setSuffix(".html");
        this.mockMvc =
                MockMvcBuilders.standaloneSetup(this.controllerUnderTest)
                        .setViewResolvers(viewResolver)
                        .build();
    }

    @Test
    public void doSomething1Test() throws Exception {
        this.mockMvc.perform(get("/home/test1"))
        .andExpect(status().isOk())
        .andExpect(model().attribute("attribute1", contains("foo")));   
    }

    @Test
    public void doSomething2Test() throws Exception {
        this.mockMvc.perform(get("/home/test2"))
        .andExpect(status().isOk()));   
    }
}

1 个答案:

答案 0 :(得分:0)

这是我在使用Spring Mobile时测试不同设备类型的方法。这不完全是你问的。可能会提供一种不同的方法来处理同样的问题。

@Test
public void getSpringMobileDesktopView() throws Exception {
    this.mockMvc.perform(get(url)
            .header("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36")
            .accept(MediaType.TEXT_HTML))
            .andExpect(view().name(containsString("desktop")))
            .andExpect(status().isOk())
            .andExpect(content().contentType(mediaTypeHtml))
            .andExpect(content().string(containsString(contains)));
}

@Test
public void getSpringMobileTabletView() throws Exception {
    this.mockMvc.perform(get(url)
            .header("user-agent", "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25")
            .accept(MediaType.TEXT_HTML))
            .andExpect(view().name(containsString("tablet")))
            .andExpect(status().isOk())
            .andExpect(content().contentType(mediaTypeHtml))
            .andExpect(content().string(containsString(contains)));
}

@Test
public void getSpringMobileMobileView() throws Exception {
    this.mockMvc.perform(get(url)
            .header("user-agent", "Mozilla/5.0 (iPhone; U; ru; CPU iPhone OS 4_2_1 like Mac OS X; ru) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148a Safari/6533.18.5")
            .accept(MediaType.TEXT_HTML))
            .andExpect(view().name(containsString("mobile")))
            .andExpect(status().isOk())
            .andExpect(content().contentType(mediaTypeHtml))
            .andExpect(content().string(containsString(contains)));
}