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); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)