libik
libik

Reputation: 23029

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?

Upvotes: 0

Views: 1228

Answers (1)

pomkine
pomkine

Reputation: 1615

Try mockMvc.perform(get("/api/01/status")).andExpect((status().isOk()));

Upvotes: 1

Related Questions