Merge pull request #8134 from alimate/BAEL-3263

BAEL-3263: Fix the Integrations Tests in spring-protobuf
This commit is contained in:
Eric Martin 2019-11-02 12:21:07 -05:00 committed by GitHub
commit 6ec3e1c08f
3 changed files with 22 additions and 19 deletions

View File

@ -1,11 +1,7 @@
package com.baeldung.protobuf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.io.InputStream;
import com.baeldung.protobuf.BaeldungTraining.Course;
import com.googlecode.protobuf.format.JsonFormat;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
@ -15,33 +11,38 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.RestTemplate;
import com.baeldung.protobuf.BaeldungTraining.Course;
import com.googlecode.protobuf.format.JsonFormat;
import java.io.IOException;
import java.io.InputStream;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
@DirtiesContext
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class ApplicationIntegrationTest {
private static final String COURSE1_URL = "http://localhost:8080/courses/1";
@Autowired
private RestTemplate restTemplate;
@LocalServerPort
private int port;
@Test
public void whenUsingRestTemplate_thenSucceed() {
ResponseEntity<Course> course = restTemplate.getForEntity(COURSE1_URL, Course.class);
ResponseEntity<Course> course = restTemplate.getForEntity(getUrl(), Course.class);
assertResponse(course.toString());
}
@Test
public void whenUsingHttpClient_thenSucceed() throws IOException {
InputStream responseStream = executeHttpRequest(COURSE1_URL);
InputStream responseStream = executeHttpRequest(getUrl());
String jsonOutput = convertProtobufMessageStreamToJsonString(responseStream);
assertResponse(jsonOutput);
}
@ -74,4 +75,8 @@ public class ApplicationIntegrationTest {
assertThat(response, containsString("number"));
assertThat(response, containsString("type"));
}
private String getUrl() {
return "http://localhost:" + port + "/courses/1";
}
}

View File

@ -1,16 +1,15 @@
package org.baeldung;
import com.baeldung.protobuf.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.protobuf.Application;
@DirtiesContext
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SpringContextIntegrationTest {
@Test

View File

@ -1,16 +1,15 @@
package org.baeldung;
import com.baeldung.protobuf.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.protobuf.Application;
@DirtiesContext
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SpringContextTest {
@Test