简体   繁体   中英

Spring testing controller does not work

I have following class :

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()));
    }

}

And I get following error : Failed tests: testStatus(cz.coffeeexperts.feedback.server.web.controller.CampaignControllerTest): Status expected:<200> but was:<404>

404 is (usually) thrown, if you type wrong address.

This is my 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());
    }
}

This address I try to test works fine when I deploy this application on tomcat. There should not be problem inside that method, because in that case it returns 500 internal error, not 404.

Am I doing something wrong?

尝试mockMvc.perform(get(“ / api / 01 / status”))。andExpect((status()。isOk()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM