RestAssuredMockMvc - 获得404

时间:2017-03-23 15:50:21

标签: java testing rest-assured

我正在尝试从常规MockMvc迁移到RestAssuredMockMvc以获取Spring Web应用程序,但我遇到了一个问题:

当尝试对一个简单的资源进行测试时,我得到了一个404.我也写了一个等效的MockMvc测试,这个测试成功了。

以下是两项测试:

RestAssured:

package at.mycompany.iad.remote.internalorg.v1.controller;

import at.mycompany.iad.remote.internalorg.v1.resource.OrgProfileResource;
import at.mycompany.iad.remote.internalorg.v1.testconfig.TestConfig;
import io.restassured.module.mockmvc.RestAssuredMockMvc;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.WebApplicationContext;

import static io.restassured.RestAssured.when;

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfig.class)
public class OrgProfileControllerITRestAssured {
    @Autowired
    private WebApplicationContext context;

    @Before
    public void initRestAssured() {
        RestAssuredMockMvc.webAppContextSetup(context);
        // RestAssuredMockMvc.standaloneSetup(new OrgProfileResource(null)); //I tried this too, same result
    }

    @Test
    public void getAd() throws Exception {

        when()
                .get("/examplepartnerid/profile")
        .then()
                .statusCode(HttpStatus.OK.value());
    }
}

和MockMvc:

package at.mycompany.iad.remote.internalorg.v1.controller;

import at.mycompany.iad.remote.internalorg.v1.testconfig.TestConfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfig.class)
public class OrgProfileControllerITMockMvc {
    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
    }

    @Test
    public void testGet() throws Exception {
        MvcResult mvcResult = this.mockMvc.perform(
                get("/examplepartnerid/profile"))
                .andExpect(status().is(200)).andReturn();
    }
}

正在测试的控制器:

package at.mycompany.iad.remote.internalorg.v1.resource;


import at.mycompany.iad.remote.internalorg.v1.controller.OrgProfileController;
import at.mycompany.iad.remote.internalorg.v1.dto.OrgProfileDto;
import io.swagger.annotations.*;
import no.finntech.kernel.framework.exception.SystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
@RequestMapping(value = "/{partnerId}/profile")
@Api(description = "Operations used for updating organisation services")
public class OrgProfileResource {


    private final OrgProfileController controller;

    @Autowired
    public OrgProfileResource(OrgProfileController controller) {
        this.controller = controller;
    }

    @RequestMapping(method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "get organisation profile")
    @ApiResponses({
            @ApiResponse(code = 200, message = "returns organisation profile for supplied partnerId"),
            @ApiResponse(code = 404, message = "no organisation found with supplied partnerId")
    })
    public ResponseEntity<OrgProfileDto> getOrgProfile(
            @ApiParam(required = true) @PathVariable String partnerId
    ) throws SystemException {
        OrgProfileDto orgProfile = controller.getOrgProfile(partnerId);
        return ResponseEntity.status(HttpStatus.OK).body(orgProfile);
    }

    @RequestMapping(method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.ACCEPTED)
    @ApiOperation(value = "get organisation services")
    @ApiResponses({
            @ApiResponse(code = 204, message = "saved organisation services for supplied partnerId"),
            @ApiResponse(code = 404, message = "no organisation found with supplied partnerId")
    })
    public void postOrgProfileServices(
            @ApiParam(required = true) @PathVariable String partnerId,
            @RequestBody OrgProfileDto requestDto
    ) throws SystemException {

        controller.saveOrgProfile(partnerId, requestDto);

    }

    @RequestMapping(value = "/image", method = RequestMethod.POST, headers = "content-type=multipart/form-data", consumes = "multipart/form-data")
    @ApiOperation(value = "Sets an image to existing organisation")
    @ApiResponses({
            @ApiResponse(code = 201, message = "The reference to the image within mycompany IAd system"),
            @ApiResponse(code = 404, message = "no organisation found for given org-name")
    })
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity addImageToProfile(
            @ApiParam(required = true) @PathVariable String partnerId,
            HttpServletRequest request) throws IOException, SystemException {

        controller.saveImage(partnerId, request);
        return ResponseEntity.status(202).build();
    }


}

0 个答案:

没有答案