如何测试具有假冒客户的rest控制器?

时间:2019-03-27 19:01:02

标签: spring spring-boot spring-boot-test spring-cloud-feign netflix-feign

我有一个使用2个fein客户端的rest控制器,我想用不同的示例编写和测试Rest控制器,我不是编写springboot测试的专家。

在这种情况下,我没有要测试的存储库,只有通过rest控制器访问的伪装客户端。下面是我的测试控制器代码

export type Test = {
    someValue: string,
    someOtherValue: string,
    yetAnotherValue: string
};

我看到的大多数测试都注入了存储库,但就我而言 我没有那些,只是假装客户,我做错了,

下面是我目前的考试

@RestController
public class CustomerController {

    @Autowired
    private CustomerClient customerClient;

    @Autowired
    private PaymentsClient paymentsClient;

    @RequestMapping(path = "/getAllCustomers", method = RequestMethod.GET)
    public ResponseEntity<Object> getAllCustomers() {
        List<Customer> customers = customerClient.getAllCustomers();
        return new ResponseEntity<>(customers, HttpStatus.OK);

    }

    @RequestMapping(path = "/{customerId}", method = RequestMethod.GET)
    public ResponseEntity<Object> get(@PathVariable() long customerId) {
        try {
            Customer c = customerClient.getCustomerById(customerId);
            if (c != null) {
                return new ResponseEntity<>(c, HttpStatus.OK);
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Customer Not Found");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }

    @RequestMapping(path = "/{customerId}", method = RequestMethod.PATCH)
    public ResponseEntity<Object> UpdateCustomer(@PathVariable() Long customerId, @RequestBody Customer customer) {
        Customer c;
        try {
            c = customerClient.update(customerId, customer);
            if (c != null) {
                return new ResponseEntity<>(c, HttpStatus.OK);
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Customer Not Found");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }

    @RequestMapping(path = "", method = RequestMethod.POST)
    public ResponseEntity<Object> saveCustomer(@RequestBody Customer customer) {
        Customer c;
        try {
            c = customerClient.saveCustomer(customer);
            return new ResponseEntity<>(c, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }

    @RequestMapping(path = "/registerPayment", method = RequestMethod.POST)
    public ResponseEntity<Object> saveCustomer(@RequestBody Payment payment) {
        Payment p = null;
        Customer c = null;
        try {
            c = customerClient.getCustomerById(payment.getCustomerId());
            p = paymentsClient.saveCustomer(payment);
            return new ResponseEntity<>(p, HttpStatus.OK);
        } catch (Exception e) {
            if (null == c) {
                return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("Customer Does not Exist");
            } else {
                e.printStackTrace();
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
            }
        }
    }

我在下面遇到错误

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class CustomerControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @InjectMocks
    private CustomerController customerController;

    @Before
    public void setup() {

        mockMvc = MockMvcBuilders.standaloneSetup(customerController).build();
    }

    @Test
    public void getAllCustomers() {

        try {
            this.mockMvc.perform(get("/getAllCustomers")).andExpect(status().isOk())
                    .andExpect(content().json("[{\n" + "    \"customerId\": 24,\n"
                            + "    \"firstName\": \"Benjamin\",\n" + "    \"secondName\": \" Masiga\",\n"
                            + "    \"email\": \"ben@ben.com\"\n" + "  }"));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

2 个答案:

答案 0 :(得分:1)

就像编写任何其他JUnit测试一样简单。

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class CustomerControllerTest {

    @Mock
    private CustomerClient customerClient;

    @InjectMocks
    private CustomerController customerController;

    @Test
    public void getAllCustomers() {
        List<Customer> customers = new ArrayList<>();
        customers.add(new Customers("name"));
        Mockito.when(customerClient.getAllCustomers()).thenReturn(customers);
        Mockito.assertEquals(customers.toString(),customerController.getAllCustomers())
    }

}

答案 1 :(得分:0)

您需要自己的feign.Client,它可以在MockMvc之外运行:

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.function.Function.identity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.request;

import feign.Client;
import feign.Request;
import feign.Response;
import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;

public class MockMvcFeignClient implements Client {

    private final MockMvc mockMvc;

    public MockMvcFeignClient(MockMvc mockMvc) {
        this.mockMvc = mockMvc;
    }

    @Override
    public Response execute(Request request, Request.Options options) {
        URI requestUrl = URI.create(request.url());
        List<String> uriVars = new ArrayList<>();
        String urlTemplate = fillVarsAndGetUrlTemplate(requestUrl, uriVars);
        HttpMethod method = HttpMethod.valueOf(request.method());
        byte[] body = request.body();
        HttpHeaders httpHeaders = convertHeaders(request);

        MockHttpServletRequestBuilder requestBuilder = request(method, urlTemplate, uriVars.toArray())
                .headers(httpHeaders)
                .content(body);

        MockHttpServletResponse resp;
        try {
            ResultActions resultActions = mockMvc.perform(requestBuilder);
            resp = resultActions.andReturn()
                    .getResponse();
        } catch (Exception e) {
            throw new IllegalStateException("Error while executing request", e);
        }

        return convertResponse(request, resp);
    }

    static String fillVarsAndGetUrlTemplate(URI requestUrl, List<String> uriVars) {
        StringBuilder urlTemplate = new StringBuilder(requestUrl.getPath());
        if (requestUrl.getQuery() != null) {
            urlTemplate.append('?');
            String[] pairs = requestUrl.getRawQuery().split("&");
            for (int i = 0; i < pairs.length; i++) {
                String pair = pairs[i];
                int separator = pair.indexOf('=');
                String paramName;
                String paramValue;
                if (separator < 0) {
                    paramName = pair;
                    paramValue = null;
                } else {
                    paramName = pair.substring(0, separator);
                    try {
                        paramValue = URLDecoder.decode(pair.substring(separator + 1), UTF_8.name());
                    } catch (UnsupportedEncodingException e) {
                        throw new UncheckedIOException(e);
                    }
                }
                urlTemplate.append(i == 0 ? paramName : "&" + paramName);
                if (paramValue != null) {
                    urlTemplate.append("={").append(paramName).append('}');
                    uriVars.add(paramValue);
                }
            }
        }
        return urlTemplate.toString();
    }

    private static HttpHeaders convertHeaders(Request request) {
        HttpHeaders headers = new HttpHeaders();
        request.headers().forEach((header, values) -> headers.put(header, new ArrayList<>(values)));
        return headers;
    }

    private static Response convertResponse(Request request, MockHttpServletResponse resp) {
        return Response.builder()
                .request(request)
                .status(resp.getStatus())
                .body(resp.getContentAsByteArray())
                .headers(resp.getHeaderNames().stream()
                        .collect(Collectors.toMap(identity(), resp::getHeaders)))
                .build();
    }
}

并使用它创建您的伪装客户:

return Feign.builder()
        .client(new MockMvcFeignClient(mockMvc))
        ...

因此,对于可以进行MockMvc集成测试的测试,现在可以通过伪装客户端调用它。