我有以下课程:
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import server.testing.WebTest;
public class CampaignControllerTest extends WebTest {
@Autowired
CampaignController campaignController;
private MockMvc mockMvc;
@Before
public void mySetup() throws Exception {
this.mockMvc = MockMvcBuilders.standaloneSetup(campaignController).build();
}
@Test
public void testStatus() throws Exception{
mockMvc.perform(get("/01/status")).andExpect((status().isOk()));
}
}
我收到以下错误:Failed tests: testStatus(cz.coffeeexperts.feedback.server.web.controller.CampaignControllerTest): Status expected:<200> but was:<404>
404(通常)抛出。
这是我的CampaingController:
@Controller
@RequestMapping(value = "/api")
public class CampaignController {
@RequestMapping(value = "/01/status", method = RequestMethod.GET)
@ResponseBody
public ServerStatusJSON getStatus() {
return new ServerStatusJSON(activeCampaignService.getActiveCampaign().getIdCampaign());
}
}
当我在tomcat上部署此应用程序时,我尝试测试的这个地址工作正常。该方法内部应该没有问题,因为在这种情况下它会返回500内部错误,而不是404。
我做错了吗?
答案 0 :(得分:1)
尝试mockMvc.perform(get(“/ api / 01 / status”))。andExpect((status()。isOk()));