Sample code to use TestRestTemplate in JUnit Test.
package com.example.rest;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import com.example.CouchBaseApplication;
import com.example.model.Student;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = CouchBaseApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ViewControllerTest {
@LocalServerPort
private int port;
private TestRestTemplate testRestTemplate = new TestRestTemplate();
List<Student> students = new ArrayList<>();
HttpHeaders headers = new HttpHeaders();
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
@Before
public void setup() {
//MockitoAnnotations.initMocks(this);
Student student = new Student("101", "Sapthika", 2, null);
students.add(student);
Student student2 = new Student("101", "Sapthika", 2, null);
students.add(student2);
}
private String createURLWithPort(String uri) {
return "http://localhost:" + port + uri;
}
@Test
public void testGetSecondHighestAge() {
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
ResponseEntity<Integer> response = testRestTemplate.exchange(
createURLWithPort("/agetest"),
HttpMethod.GET, entity, Integer.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
System.out.println("Response : "+response.getBody());
assertEquals(Integer.valueOf(44), response.getBody());
}
@Test
public void testGetCount() {
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
ResponseEntity<Long> response = testRestTemplate.exchange(
createURLWithPort("/count"),
HttpMethod.GET, entity, Long.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
System.out.println("Count value : "+response.getBody());
assertEquals(Long.valueOf(5), response.getBody());
}
@Test
public void testGetByCourseName() {
ResponseEntity<List<Student>> response = testRestTemplate.exchange(
createURLWithPort("/students/course/name/{courseName}"),
HttpMethod.GET, entity, new ParameterizedTypeReference<List<Student>>(){},"Java");
assertEquals(HttpStatus.OK, response.getStatusCode());
System.out.println("Response : "+ response.getBody());
assertEquals(2, response.getBody().size());
}
@Test
public void testGetByCourseId() {
ResponseEntity<List<Student>> response = testRestTemplate.exchange(
createURLWithPort("/students/course/id/{courseId}"),
HttpMethod.GET, entity, new ParameterizedTypeReference<List<Student>>(){},"100");
assertEquals(HttpStatus.OK, response.getStatusCode());
System.out.println("Response : "+ response.getBody());
//assertEquals(2, response.getBody().size());
assertEquals("Asserting reponse body size", 2, response.getBody().size());
}
@Test
public void testRetrieveStudentCourse() {
HttpHeaders headers = new HttpHeaders();
HttpEntity<Student> entity = new HttpEntity<>(new Student("101", "rja", 5, null));
ResponseEntity<String> response = testRestTemplate.exchange(
createURLWithPort("/test"),
HttpMethod.GET, entity, String.class);
Student student = new Student("101", "Sapthika", 2, null);
ResponseEntity<Student> response1 = testRestTemplate.exchange(
createURLWithPort("/students"),
HttpMethod.POST, entity, Student.class);
HttpEntity<String> entity1 = new HttpEntity<String>("42", headers);
ResponseEntity<Student> response2 = testRestTemplate.exchange(
createURLWithPort("/students/{id}"),
HttpMethod.PUT,entity1,Student.class,"42");
String expected = "{id:Course1,name:Spring,description:10 Steps}";
assertEquals(HttpStatus.CREATED, response.getStatusCode());
assertEquals("Response from API", response.getBody());
//JSONAssert.assertEquals(expected, response.getBody(), false);
}
@Test
public void testPutMethod() {
HttpEntity<Student> entity = new HttpEntity<Student>(new Student("15", "John", 99,null),headers);
ResponseEntity<Student> response = testRestTemplate.exchange(
createURLWithPort("/students"),
HttpMethod.PUT, entity, Student.class);
System.out.println(response.getBody());
assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
}
@Test
public void testPutMethod1() {
HttpEntity<Student> entity = new HttpEntity<Student>(new Student("15", "xxxx", 99,null),headers);
ResponseEntity<Student> response = testRestTemplate.exchange(
createURLWithPort("/students"),
HttpMethod.PUT, entity, Student.class);
System.out.println(response.getBody());
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
}
@Test
public void testAddNew() throws JSONException {
ResponseEntity<Iterable<Student>> response = testRestTemplate.exchange(
createURLWithPort("/get/{name}"),
HttpMethod.GET, entity, new ParameterizedTypeReference<Iterable<Student>>(){},"John");
System.out.println("Body : "+response.getBody());
assertEquals(HttpStatus.OK, response.getStatusCode());
List<Student> studentList = new ArrayList<>();
response.getBody().forEach(studentList::add);
assertEquals(5,studentList.size());
//JSONAssert.assertEquals(expected, response.getBody(), false);
}
}
package com.example.rest;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import com.example.CouchBaseApplication;
import com.example.model.Student;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = CouchBaseApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ViewControllerTest {
@LocalServerPort
private int port;
private TestRestTemplate testRestTemplate = new TestRestTemplate();
List<Student> students = new ArrayList<>();
HttpHeaders headers = new HttpHeaders();
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
@Before
public void setup() {
//MockitoAnnotations.initMocks(this);
Student student = new Student("101", "Sapthika", 2, null);
students.add(student);
Student student2 = new Student("101", "Sapthika", 2, null);
students.add(student2);
}
private String createURLWithPort(String uri) {
return "http://localhost:" + port + uri;
}
@Test
public void testGetSecondHighestAge() {
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
ResponseEntity<Integer> response = testRestTemplate.exchange(
createURLWithPort("/agetest"),
HttpMethod.GET, entity, Integer.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
System.out.println("Response : "+response.getBody());
assertEquals(Integer.valueOf(44), response.getBody());
}
@Test
public void testGetCount() {
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
ResponseEntity<Long> response = testRestTemplate.exchange(
createURLWithPort("/count"),
HttpMethod.GET, entity, Long.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
System.out.println("Count value : "+response.getBody());
assertEquals(Long.valueOf(5), response.getBody());
}
@Test
public void testGetByCourseName() {
ResponseEntity<List<Student>> response = testRestTemplate.exchange(
createURLWithPort("/students/course/name/{courseName}"),
HttpMethod.GET, entity, new ParameterizedTypeReference<List<Student>>(){},"Java");
assertEquals(HttpStatus.OK, response.getStatusCode());
System.out.println("Response : "+ response.getBody());
assertEquals(2, response.getBody().size());
}
@Test
public void testGetByCourseId() {
ResponseEntity<List<Student>> response = testRestTemplate.exchange(
createURLWithPort("/students/course/id/{courseId}"),
HttpMethod.GET, entity, new ParameterizedTypeReference<List<Student>>(){},"100");
assertEquals(HttpStatus.OK, response.getStatusCode());
System.out.println("Response : "+ response.getBody());
//assertEquals(2, response.getBody().size());
assertEquals("Asserting reponse body size", 2, response.getBody().size());
}
@Test
public void testRetrieveStudentCourse() {
HttpHeaders headers = new HttpHeaders();
HttpEntity<Student> entity = new HttpEntity<>(new Student("101", "rja", 5, null));
ResponseEntity<String> response = testRestTemplate.exchange(
createURLWithPort("/test"),
HttpMethod.GET, entity, String.class);
Student student = new Student("101", "Sapthika", 2, null);
ResponseEntity<Student> response1 = testRestTemplate.exchange(
createURLWithPort("/students"),
HttpMethod.POST, entity, Student.class);
HttpEntity<String> entity1 = new HttpEntity<String>("42", headers);
ResponseEntity<Student> response2 = testRestTemplate.exchange(
createURLWithPort("/students/{id}"),
HttpMethod.PUT,entity1,Student.class,"42");
String expected = "{id:Course1,name:Spring,description:10 Steps}";
assertEquals(HttpStatus.CREATED, response.getStatusCode());
assertEquals("Response from API", response.getBody());
//JSONAssert.assertEquals(expected, response.getBody(), false);
}
@Test
public void testPutMethod() {
HttpEntity<Student> entity = new HttpEntity<Student>(new Student("15", "John", 99,null),headers);
ResponseEntity<Student> response = testRestTemplate.exchange(
createURLWithPort("/students"),
HttpMethod.PUT, entity, Student.class);
System.out.println(response.getBody());
assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
}
@Test
public void testPutMethod1() {
HttpEntity<Student> entity = new HttpEntity<Student>(new Student("15", "xxxx", 99,null),headers);
ResponseEntity<Student> response = testRestTemplate.exchange(
createURLWithPort("/students"),
HttpMethod.PUT, entity, Student.class);
System.out.println(response.getBody());
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
}
@Test
public void testAddNew() throws JSONException {
ResponseEntity<Iterable<Student>> response = testRestTemplate.exchange(
createURLWithPort("/get/{name}"),
HttpMethod.GET, entity, new ParameterizedTypeReference<Iterable<Student>>(){},"John");
System.out.println("Body : "+response.getBody());
assertEquals(HttpStatus.OK, response.getStatusCode());
List<Student> studentList = new ArrayList<>();
response.getBody().forEach(studentList::add);
assertEquals(5,studentList.size());
//JSONAssert.assertEquals(expected, response.getBody(), false);
}
}