从测试用例调用控制器时,使用自动连接的组件测试控制器为空

从测试用例调用控制器时,使用自动连接的组件测试控制器为空,第1张

测试用例调用控制器时,使用自动连接的组件测试控制器为空

Spring不会自动线的组成部分,因为你实例化你的控制器, 新的 不使用Spring,因此组件不instatntiated

SpringMockMvc测试检查它是否正确:

@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTestpublic class CreateTest {    @Autowired    private WebApplicationContext context;    private MockMvc mvc;    @Before    public void setup() {        mvc = MockMvcBuilders     .webAppContextSetup(context)     .build();    }    @Test    public void testCall() throws Exception {        //increment first time        this.mvc.perform(get("/greeting"))     .andExpect(status().isOk());        //increment secont time and get response to check        String contentAsString = this.mvc.perform(get("/greeting"))     .andExpect(status().isOk()).andReturn()     .getResponse().getContentAsString();        assertEquals("Hello World 2", contentAsString);    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5641491.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存