【问题标题】:Mock device for controller test控制器测试模拟装置
【发布时间】:2017-05-15 13:30:28
【问题描述】:

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

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

第二个控制器方法期望设备在从浏览器调用时工作,但在单元测试中。当调用测试方法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 正确实例化但 device 没有?有没有办法在 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()));   
    }
}

【问题讨论】:

  • 你解决了吗?我和你的情况一样,我无法在单元测试中注入设备..
  • 不,但如果您找到解决方案,请告诉我。

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


【解决方案1】:

这就是我在使用 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)));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 2018-12-01
    • 2013-05-03
    • 1970-01-01
    相关资源
    最近更新 更多