如何测试假装客户端的休息控制器?

问题描述 投票:0回答:1

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

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

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

我见过的大多数测试都注入了一个存储库,但对于我的情况我没有那些,只是fein客户端,我做错了,

以下是我目前的测试

@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\": \"[email protected]\"\n" + "  }"));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

我收到以下错误,

NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.test.web.servlet.MockMvc' available: expected at least 1 bean which qualifies as autowire candidate. 
spring spring-boot spring-boot-test spring-cloud-feign netflix-feign
1个回答
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())
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.