美文网首页
Spring框架中模拟RestTemplate

Spring框架中模拟RestTemplate

作者: 一路花开_8fab | 来源:发表于2019-04-16 19:55 被阅读0次

    该系列文章翻译自https://www.baeldung.com/mockito-series

    下面介绍模拟RestTemplate的两种方式

    @Service
    public class EmployeeService {
    
        @Autowired
        private RestTemplate restTemplate;
    
        public Employee getEmployee(String id) {
    
        ResponseEntity resp = 
    
              restTemplate.getForEntity("http://localhost:8080/employee/" + id, Employee.class);
    
        return resp.getStatusCode() == HttpStatus.OK ? resp.getBody() : null;
    
        }
    
    }
    
    

    使用Mockito

    @RunWith(MockitoJUnitRunner.class)
    public class EmployeeServiceTest {
     
        @Mock
        private RestTemplate restTemplate;
     
        @InjectMocks
        private EmployeeService empService = new EmployeeService();
     
        @Test
        public void givenMockingIsDoneByMockito_whenGetIsCalled_shouldReturnMockedObject() {
            Employee emp = new Employee(“E001”, "Eric Simmons");
            Mockito
              .when(restTemplate.getForEntity("http://localhost:8080/employee/E001", Employee.class))
              .thenReturn(new ResponseEntity(emp, HttpStatus.OK));
     
            Employee employee = empService.getEmployee(id);
            Assert.assertEquals(emp, employee);
        }
    }
    
    

    2. 使用Spring Test

    SpringTest模块包含一个名为MockRestServiceServer的mock服务器,当请求传递给RestTemplate时,我们可以设置服务器返回指定的对象。

    @RunWith(SpringRunner.class)
    @ContextConfiguration(classes = SpringTestConfig.class)
    public class EmployeeServiceMockRestServiceServerUnitTest {
    
        @Autowired
        private EmployeeService empService;
        @Autowired
        private RestTemplate restTemplate;
       
        private MockRestServiceServer mockServer;
        private ObjectMapper mapper = **new** ObjectMapper();
    
        @Before
        public void init() {
            mockServer = MockRestServiceServer.createServer(restTemplate);
        }
    
        @Test                                                                                         
        public void givenMockingIsDoneByMockRestServiceServer_whenGetIsCalled_thenReturnsMockedObject()() {   
            Employee emp = new Employee("E001", "Eric Simmons");
            mockServer.expect(ExpectedCount.once(), 
              requestTo(new  URI("http://localhost:8080/employee/E001")))
              .andExpect(method(HttpMethod.GET))
              .andRespond(withStatus(HttpStatus.OK)
              .contentType(MediaType.APPLICATION_JSON)
              .body(mapper.writeValueAsString(emp))
            );                                   
    
            Employee employee = empService.getEmployee(id);
            mockServer.verify();
            Assert.assertEquals(emp, employee);                                                        
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Spring框架中模拟RestTemplate

          本文链接:https://www.haomeiwen.com/subject/wluiwqtx.html