Java 如何使用RestTemplate和JUnit测试restclient?

Java 如何使用RestTemplate和JUnit测试restclient?,java,rest,spring-boot,junit,resttemplate,Java,Rest,Spring Boot,Junit,Resttemplate,我是JUNIT新手,使用RestTemplate调用我的服务,我得到了200个响应。但是,我不能使用JUnit测试这个类。尝试了不同的方法,得到了400和404。我想发布请求主体(json)并测试状态。如果有任何问题,请告诉我 /** * Rest client implementation **/ public class CreateEmailDelegate implements CDM { @Autowired private RestTempla

我是JUNIT新手,使用
RestTemplate
调用我的服务,我得到了200个响应。但是,我不能使用JUnit测试这个类。尝试了不同的方法,得到了400和404。我想发布请求主体(json)并测试状态。如果有任何问题,请告诉我

/**
* Rest client implementation
**/  
public class CreateEmailDelegate implements CDM {
         
    @Autowired
    private RestTemplate restTemplate;
    private  String url = "http://example.com/communications/emails";
        
    public ResponseEntity<CDResponse> createEmail(CDMEmailRequest cDRequest) throws UnavailableServiceException, InvalidInputException {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("SR_API_Key", SR_API_KEY);
        httpHeaders.set("consumerIdentification", CONSUMER_IDENTIFICATION);
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);         
        HttpEntity< CDMEmailRequest > cDRequestEntity = new HttpEntity<>( cDRequest, httpHeaders);  
        ResponseEntity< CDResponse > cDResponse = null;
                
        try {
            cDResponse = restTemplate.postForEntity(url, cDRequestEntity, CDResponse.class);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            throw  e;       
        }
                
        return cDResponse;
    }
}  
我的测试类使用
testrestemplate
代替
MockMvc
返回400

@RunWith(SpringJUnit4ClassRunner.class)
public class CreateEmailCommunicationDelegateTest {

    @Before
    public void setup() {
        httpHeaders = new HttpHeaders();
        // rest headers as above
    }

    @Test
    public void testResponse() throws Exception, HttpClientErrorException, JsonProcessingException {

        String url = "http://example.com/CommunicationDeliveryManagement-Service-1.0.0/communications/emails";

        String username = "";
        String password = "";
        HttpEntity<CDMEmailRequest>
                cDEntity = new HttpEntity<>(httpHeaders);

        restTemplate = new TestRestTemplate(username, password);

        responseEntity =
                restTemplate.exchange(url, HttpMethod.POST, cDEntity,
                        CDResponse.class);

        assertNotNull(responseEntity);
        assertEquals(HttpStatus.OK,
                responseEntity.getStatusCode());
    }
} 
@RunWith(SpringJUnit4ClassRunner.class)
公共类CreateEmailCommunicationDelegateTest{
@以前
公共作废设置(){
httpHeaders=新的httpHeaders();
//其余标题如上所述
}
@试验
public void testResponse()引发异常、HttpClientErrorException、JsonProcessingException{
字符串url=”http://example.com/CommunicationDeliveryManagement-Service-1.0.0/communications/emails";
字符串username=“”;
字符串密码=”;
HttpEntity
cDEntity=新的HttpEntity(httpHeaders);
restTemplate=新的TestRestTemplate(用户名、密码);
反应性=
restemplate.exchange(url、HttpMethod.POST、cDEntity、,
CDResponse.class);
assertNotNull(responseEntity);
assertEquals(HttpStatus.OK,
responseEntity.getStatusCode());
}
} 

我认为您正在尝试实现集成测试,而不是单元测试,两者有很大区别
MockMvc
应用于实现单元测试,而
testrestemplate
应用于集成测试。您不能将其用于测试客户机实现


如果您使用的是Spring Boot,您可以使用另一种方法来实现您的目标。请使用查看此问题。

请正确格式化您的代码。我看到你在使用Mockito Runner,你也用Mockito标记了这个问题,但我在代码中没有看到与Mockito的任何交互。删除它,感谢uIt是一个外部服务?是的,它是rest服务。你不能使用MockMvc或TestRestTemplate进行RESTClient测试。请参阅我的回答谢谢你的回答。是的,我只想做集成测试(Springboot)。我将按照你提到的例子进行检查。@susan如果答案有助于你解决问题,请投票表决
@RunWith(SpringJUnit4ClassRunner.class)
public class CreateEmailCommunicationDelegateTest {

    @Before
    public void setup() {
        httpHeaders = new HttpHeaders();
        // rest headers as above
    }

    @Test
    public void testResponse() throws Exception, HttpClientErrorException, JsonProcessingException {

        String url = "http://example.com/CommunicationDeliveryManagement-Service-1.0.0/communications/emails";

        String username = "";
        String password = "";
        HttpEntity<CDMEmailRequest>
                cDEntity = new HttpEntity<>(httpHeaders);

        restTemplate = new TestRestTemplate(username, password);

        responseEntity =
                restTemplate.exchange(url, HttpMethod.POST, cDEntity,
                        CDResponse.class);

        assertNotNull(responseEntity);
        assertEquals(HttpStatus.OK,
                responseEntity.getStatusCode());
    }
}