How To Write Junit Test Cases For Web Services
This guide volition help you write great unit tests for your Bound Boot Rest Service. We will apply a simple code example creating couple of elementary remainder services.
Yous will acquire
- What is Unit Testing?
- How to create a Get REST Service for retrieving the courses that a pupil registered for?
- How to write a unit of measurement test for Get REST Service?
- How to create a Mail REST Service for registering a course for student?
- How to write a unit test for POST Service?
Free Courses - Learn in ten Steps
- FREE v DAY Challenge - Acquire Bound and Spring Boot
- Learn Spring Boot in x Steps
- Learn Docker in 10 Steps
- Learn Kubernetes in x Steps
- Learn AWS in ten Steps
- Maven 3.0+ is your build tool
- Your favorite IDE. We utilize Eclipse.
- JDK 1.8+
Complete Maven Project With Code Examples
Our Github repository has all the code examples - https://github.com/in28minutes/in28minutes.github.io/tree/master/code-naught-files
- Rest Services with Unit and Integration Tests
- Website-springbootrestservices-simplerestserviceswithunitandintegrationtests.zip
Unit Testing
Following screenshot shows eclipse project with all the files we will create.
We want to create a unit of measurement test for StudentController
which is a Residual Controller. StudentController
exposes two service methods - one Get and one Post. We will write unit of measurement tests for both these service methods.
In the unit test
- We will mock out the StudentService using Mockito
- We will utilize Mock MVC framework to launch simply StudentController.
A central part of unit testing is to restrict the scope to a minimum. In this unit examination, nosotros desire to examination only the methods in StudentController
.
Overview
Following is the society we do things in this guide:
- Bootstrap a project using Bound Initializr.
- Implement a Business Service for our API - StudentService.
- Implement the API - using StudentController. Kickoff we implement the Get methods and and so the POST methods.
- Unit Examination the API.
Bootstrap REST Services Application with Spring Initializr
Spring Initializr http://beginning.bound.io/ is great tool to bootstrap your Leap Kicking projects.
As shown in the prototype above, following steps have to be done
- Launch Spring Initializr and choose the following
- Cull
com.in28minutes.springboot
as Group - Cull
student-services
as Artifact - Choose following dependencies
- Web
- Actuator
- DevTools
- Cull
- Click Generate Project.
- Import the project into Eclipse.
- If you want to understand all the files that are role of this project, you can go here.
Implementing Concern Service for your Application
All applications need data. Instead of talking to a real database, we will use an ArrayList
- kind of an in-memory information store.
A student tin can take multiple courses. A course has an id, name, clarification and a list of steps you demand to consummate to finish the course. A student has an id, proper name, clarification and a listing of courses he/she is currently registered for. Nosotros have StudentService exposing methods to
-
public List<Student> retrieveAllStudents()
- Call up details for all students -
public Student retrieveStudent(String studentId)
- Think a specific educatee details -
public Listing<Form> retrieveCourses(String studentId)
- Retrieve all courses a student is registered for -
public Course retrieveCourse(Cord studentId, String courseId)
- Retrieve details of a specific grade a pupil is registered for -
public Course addCourse(String studentId, Course grade)
- Add together a course to an existing student
Refer to these files at the bottom of the article for exact implementation of the Service StudentService
and the model classes Course
and Student
.
- src/main/java/com/in28minutes/springboot/model/Course.coffee
- src/main/coffee/com/in28minutes/springboot/model/Pupil.java
- src/main/java/com/in28minutes/springboot/service/StudentService.java
Adding Couple of GET Residue Services
The Rest Service StudentController
exposes couple of go services.
-
@Autowired private StudentService studentService
: We are using Spring Autowiring to wire the student service into the StudentController. -
@GetMapping("/students/{studentId}/courses")
: Exposing a Become Service with studentId as a path variable -
@GetMapping("/students/{studentId}/courses/{courseId}")
: Exposing a Get Service for retrieving specific course of a student. -
@PathVariable String studentId
: Value of studentId from the uri volition be mapped to this parameter.
package com.in28minutes.springboot.controller ; import java.util.List ; import org.springframework.beans.manufactory.annotation.Autowired ; import org.springframework.web.demark.note.GetMapping ; import org.springframework.web.bind.notation.PathVariable ; import org.springframework.web.bind.annotation.RestController ; import com.in28minutes.springboot.model.Course ; import com.in28minutes.springboot.service.StudentService ; @RestController public class StudentController { @Autowired individual StudentService studentService ; @GetMapping ( "/students/{studentId}/courses" ) public List < Course > retrieveCoursesForStudent ( @PathVariable String studentId ) { return studentService . retrieveCourses ( studentId ); } @GetMapping ( "/students/{studentId}/courses/{courseId}" ) public Course retrieveDetailsForCourse ( @PathVariable String studentId , @PathVariable String courseId ) { return studentService . retrieveCourse ( studentId , courseId ); } }
Executing the Get Service Using Postman
We will burn down a asking to http://localhost:8080/students/Student1/courses/Course1 to test the service. Response is equally shown below.
{ "id" : "Course1" , "proper noun" : "Spring" , "description" : "10Steps" , "steps" : [ "Learn Maven" , "Import Project" , "First Instance" , "Second Instance" ] }
Beneath picture shows how we can execute this Get Service from Postman - my favorite tool to run rest services.
Add together spring-security-examination for disabling security in unit tests
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <telescopic>exam</telescopic> </dependency>
Unit of measurement Testing the Get Rest Service
When nosotros are unit of measurement testing a rest service, nosotros would desire to launch only the specific controller and the related MVC Components. WebMvcTest annotation is used for unit testing Spring MVC application. This can be used when a test focuses just Leap MVC components. Using this annotation will disable full auto-configuration and only apply configuration relevant to MVC tests.
-
@RunWith(SpringRunner.course)
: SpringRunner is short hand for SpringJUnit4ClassRunner which extends BlockJUnit4ClassRunner providing the functionality to launch a Spring TestContext Framework. -
@WebMvcTest(value = StudentController.form)
: WebMvcTest note is used for unit testing Leap MVC awarding. This can be used when a exam focuses only Jump MVC components. In this examination, we desire to launch only StudentController. All other controllers and mappings will not exist launched when this unit test is executed. -
@Autowired individual MockMvc mockMvc
: MockMvc is the main entry signal for server-side Leap MVC exam support. It allows us to execute requests against the exam context. -
@MockBean individual StudentService studentService
: MockBean is used to add mocks to a Jump ApplicationContext. A mock of studentService is created and machine-wired into the StudentController. -
Mockito.when(studentService.retrieveCourse(Mockito.anyString(),Mockito.anyString())).thenReturn(mockCourse)
: Mocking the method retrieveCourse to return the specific mockCourse when invoked. -
MockMvcRequestBuilders.get("/students/Student1/courses/Course1").accept(MediaType.APPLICATION_JSON)
: Creating a Request builder to be able to execute a get request to uri "/students/Student1/courses/Course1" with accept header every bit "application/json" -
mockMvc.perform(requestBuilder).andReturn()
: mockMvc is used to perform the request and return the response back. -
JSONAssert.assertEquals(expected, issue.getResponse().getContentAsString(), simulated)
: Nosotros are using org.skyscreamer.jsonassert.JSONAssert. This allows the states to do partial asserts against a JSON String. We are passing strict equally fake since we do not desire to check for all fields in the response.
package com.in28minutes.springboot.controller ; import java.util.Arrays ; import org.junit.Test ; import org.junit.runner.RunWith ; import org.mockito.Mockito ; import org.skyscreamer.jsonassert.JSONAssert ; import org.springframework.beans.factory.annotation.Autowired ; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest ; import org.springframework.boot.test.mock.mockito.MockBean ; import org.springframework.http.MediaType ; import org.springframework.examination.context.junit4.SpringRunner ; import org.springframework.test.spider web.servlet.MockMvc ; import org.springframework.test.spider web.servlet.MvcResult ; import org.springframework.test.web.servlet.RequestBuilder ; import org.springframework.examination.web.servlet.request.MockMvcRequestBuilders ; import com.in28minutes.springboot.model.Grade ; import com.in28minutes.springboot.service.StudentService ; @RunWith ( SpringRunner . class ) @WebMvcTest ( value = StudentController . class ) @WithMockUser public course StudentControllerTest { @Autowired private MockMvc mockMvc ; @MockBean private StudentService studentService ; Course mockCourse = new Course ( "Course1" , "Bound" , "10Steps" , Arrays . asList ( "Learn Maven" , "Import Project" , "First Example" , "Second Case" )); String exampleCourseJson = "{\"name\":\"Spring\",\"clarification\":\"10Steps\",\"steps\":[\"Larn Maven\",\"Import Project\",\"Outset Example\",\"2d Example\"]}" ; @Examination public void retrieveDetailsForCourse () throws Exception { Mockito . when ( studentService . retrieveCourse ( Mockito . anyString (), Mockito . anyString ())). thenReturn ( mockCourse ); RequestBuilder requestBuilder = MockMvcRequestBuilders . get ( "/students/Student1/courses/Course1" ). accept ( MediaType . APPLICATION_JSON ); MvcResult consequence = mockMvc . perform ( requestBuilder ). andReturn (); System . out . println ( result . getResponse ()); String expected = "{id:Course1,name:Jump,description:10Steps}" ; // {"id":"Course1","name":"Spring","description":"ten Steps, 25 Examples and 10K Students","steps":["Larn Maven","Import Project","First Instance","Second Case"]} JSONAssert . assertEquals ( expected , upshot . getResponse () . getContentAsString (), simulated ); } }
Calculation a POST Rest Service
A POST Service should render a condition of created (201) when the resource creation is successful.
@PostMapping("/students/{studentId}/courses")
: Mapping a url for the POST Asking @RequestBody Grade newCourse
: Using Binding to bind the trunk of the request to Course object. ResponseEntity.created(location).build()
: Return a status of created. Also return the location of created resource as a Response Header.
@PostMapping ( "/students/{studentId}/courses" ) public ResponseEntity < Void > registerStudentForCourse ( @PathVariable Cord studentId , @RequestBody Course newCourse ) { Course class = studentService . addCourse ( studentId , newCourse ); if ( grade == nix ) return ResponseEntity . noContent (). build (); URI location = ServletUriComponentsBuilder . fromCurrentRequest (). path ( "/{id}" ). buildAndExpand ( grade . getId ()). toUri (); render ResponseEntity . created ( location ). build (); }
Executing a Mail Rest Service
Example Request is shown below. It contains all the details to register a class to a student.
{ "name" : "Microservices" , "description" : "10Steps" , "steps" : [ "Learn How to Pause Things Up" , "Automate the hell out of everything" , "Accept fun" ] }
Below picture shows how we can execute this Post Service from Postman - my favorite tool to run rest services. Make certain you get to the Torso tab and select raw. Select JSON from the dropdown. Re-create in a higher place request into body.
The URL we use is http://localhost:8080/students/Student1/courses.
Writing Unit Test for the POST Residue Service
In the unit of measurement test, we would desire to postal service the request body to the url /students/Student1/courses
. In the response, we cheque for HttpStatus of Created and that the location header contains the url of the created resource.
-
MockMvcRequestBuilders.post("/students/Student1/courses").accept(MediaType.APPLICATION_JSON)
: Create a postal service request with an accept header forapplication\json
-
content(exampleCourseJson).contentType(MediaType.APPLICATION_JSON)
: Apply the specified content as body of the request and set content type header. -
assertEquals(HttpStatus.CREATED.value(), response.getStatus())
: Assert that the return status is CREATED. -
response.getHeader(HttpHeaders.LOCATION)
: Get the location from response header and later affirm that it contains the URI of the created resource.
@Examination public void createStudentCourse () throws Exception { Form mockCourse = new Course ( "1" , "Smallest Number" , "1" , Arrays . asList ( "1" , "two" , "3" , "4" )); // studentService.addCourse to respond back with mockCourse Mockito . when ( studentService . addCourse ( Mockito . anyString (), Mockito . any ( Form . course ))). thenReturn ( mockCourse ); // Send form as torso to /students/Student1/courses RequestBuilder requestBuilder = MockMvcRequestBuilders . mail service ( "/students/Student1/courses" ) . accept ( MediaType . APPLICATION_JSON ). content ( exampleCourseJson ) . contentType ( MediaType . APPLICATION_JSON ); MvcResult result = mockMvc . perform ( requestBuilder ). andReturn (); MockHttpServletResponse response = result . getResponse (); assertEquals ( HttpStatus . CREATED . value (), response . getStatus ()); assertEquals ( "http://localhost/students/Student1/courses/1" , response . getHeader ( HttpHeaders . LOCATION )); }
Complete Code Example
pom.xml
<?xml version="1.0" encoding="UTF-viii"?> <project xmlns= "http://maven.apache.org/POM/four.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-four.0.0.xsd" > <modelVersion>four.0.0</modelVersion> <groupId>com.in28minutes.springboot</groupId> <artifactId>student-services</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>student-services</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.kick</groupId> <artifactId>spring-kick-starter-parent</artifactId> <version>two.three.i.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</projection.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>ane.8</coffee.version> <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-kick-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>jump-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</telescopic> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>exam</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
src/main/java/com/in28minutes/springboot/controller/StudentController.java
packet com.in28minutes.springboot.controller ; import java.cyberspace.URI ; import java.util.List ; import org.springframework.beans.manufacturing plant.annotation.Autowired ; import org.springframework.http.ResponseEntity ; import org.springframework.web.bind.annotation.GetMapping ; import org.springframework.spider web.demark.note.PathVariable ; import org.springframework.web.demark.annotation.PostMapping ; import org.springframework.spider web.bind.annotation.RequestBody ; import org.springframework.web.demark.annotation.RestController ; import org.springframework.web.servlet.support.ServletUriComponentsBuilder ; import com.in28minutes.springboot.model.Form ; import com.in28minutes.springboot.service.StudentService ; @RestController public class StudentController { @Autowired individual StudentService studentService ; @GetMapping ( "/students/{studentId}/courses" ) public List < Class > retrieveCoursesForStudent ( @PathVariable Cord studentId ) { return studentService . retrieveCourses ( studentId ); } @GetMapping ( "/students/{studentId}/courses/{courseId}" ) public Class retrieveDetailsForCourse ( @PathVariable String studentId , @PathVariable Cord courseId ) { return studentService . retrieveCourse ( studentId , courseId ); } @PostMapping ( "/students/{studentId}/courses" ) public ResponseEntity < Void > registerStudentForCourse ( @PathVariable String studentId , @RequestBody Form newCourse ) { Form class = studentService . addCourse ( studentId , newCourse ); if ( grade == zip ) return ResponseEntity . noContent (). build (); URI location = ServletUriComponentsBuilder . fromCurrentRequest (). path ( "/{id}" ). buildAndExpand ( course . getId ()). toUri (); return ResponseEntity . created ( location ). build (); } }
src/master/java/com/in28minutes/springboot/model/Course.coffee
bundle com.in28minutes.springboot.model ; import java.util.List ; public form Course { private Cord id ; private String proper noun ; private String clarification ; individual List < String > steps ; // Needed by Caused by: com.fasterxml.jackson.databind.JsonMappingException: // Tin can non construct instance of com.in28minutes.springboot.model.Course: // no suitable constructor constitute, can non deserialize from Object value // (missing default constructor or creator, or perhaps need to add/enable // type information?) public Course () { } public Class ( Cord id , Cord name , String description , List < String > steps ) { super (); this . id = id ; this . proper noun = name ; this . description = description ; this . steps = steps ; } public Cord getId () { return id ; } public void setId ( String id ) { this . id = id ; } public String getDescription () { return description ; } public String getName () { return name ; } public Listing < String > getSteps () { render steps ; } @Override public String toString () { return String . format ( "Course [id=%s, name=%due south, description=%south, steps=%s]" , id , name , description , steps ); } @Override public int hashCode () { last int prime = 31 ; int result = i ; outcome = prime * result + (( id == null ) ? 0 : id . hashCode ()); return result ; } @Override public boolean equals ( Object obj ) { if ( this == obj ) return truthful ; if ( obj == zilch ) return false ; if ( getClass () != obj . getClass ()) return false ; Class other = ( Course ) obj ; if ( id == null ) { if ( other . id != nix ) render fake ; } else if (! id . equals ( other . id )) render false ; return true ; } }
src/main/coffee/com/in28minutes/springboot/model/Student.java
package com.in28minutes.springboot.model ; import java.util.Listing ; public class Student { individual String id ; individual String name ; individual String description ; private Listing < Course > courses ; public Student ( String id , String name , String description , List < Course > courses ) { super (); this . id = id ; this . proper name = proper name ; this . clarification = description ; this . courses = courses ; } public String getId () { return id ; } public void setId ( String id ) { this . id = id ; } public String getName () { return name ; } public void setName ( String name ) { this . proper noun = name ; } public String getDescription () { render description ; } public void setDescription ( String description ) { this . description = description ; } public List < Course > getCourses () { return courses ; } public void setCourses ( List < Class > courses ) { this . courses = courses ; } @Override public String toString () { return Cord . format ( "Pupil [id=%s, name=%due south, description=%south, courses=%s]" , id , name , clarification , courses ); } }
src/principal/java/com/in28minutes/springboot/service/StudentService.coffee
parcel com.in28minutes.springboot.service ; import java.math.BigInteger ; import java.security.SecureRandom ; import java.util.ArrayList ; import java.util.Arrays ; import java.util.List ; import org.springframework.stereotype.Component ; import com.in28minutes.springboot.model.Course ; import com.in28minutes.springboot.model.Student ; @Component public class StudentService { individual static List < Student > students = new ArrayList <>(); static { //Initialize Data Grade course1 = new Grade ( "Course1" , "Bound" , "10Steps" , Arrays . asList ( "Learn Maven" , "Import Project" , "First Example" , "Second Example" )); Course course2 = new Course ( "Course2" , "Spring MVC" , "10 Examples" , Arrays . asList ( "Learn Maven" , "Import Project" , "First Example" , "Second Example" )); Class course3 = new Class ( "Course3" , "Spring Kick" , "6K Students" , Arrays . asList ( "Acquire Maven" , "Learn Leap" , "Acquire Leap MVC" , "First Instance" , "Second Example" )); Course course4 = new Class ( "Course4" , "Maven" , "Most popular maven class on internet!" , Arrays . asList ( "Pom.xml" , "Build Life Cycle" , "Parent POM" , "Importing into Eclipse" )); Student ranga = new Student ( "Student1" , "Ranga Karanam" , "Hiker, Developer and Architect" , new ArrayList <>( Arrays . asList ( course1 , course2 , course3 , course4 ))); Student satish = new Pupil ( "Student2" , "Satish T" , "Hiker, Developer and Architect" , new ArrayList <>( Arrays . asList ( course1 , course2 , course3 , course4 ))); students . add ( ranga ); students . add ( satish ); } public List < Student > retrieveAllStudents () { render students ; } public Student retrieveStudent ( String studentId ) { for ( Student pupil : students ) { if ( student . getId (). equals ( studentId )) { render educatee ; } } return null ; } public List < Course > retrieveCourses ( String studentId ) { Pupil student = retrieveStudent ( studentId ); if ( student == zippo ) { return nil ; } render educatee . getCourses (); } public Grade retrieveCourse ( String studentId , String courseId ) { Educatee pupil = retrieveStudent ( studentId ); if ( student == null ) { render null ; } for ( Course course : student . getCourses ()) { if ( course . getId (). equals ( courseId )) { return course ; } } return null ; } private SecureRandom random = new SecureRandom (); public Course addCourse ( String studentId , Course form ) { Educatee pupil = retrieveStudent ( studentId ); if ( student == null ) { return cipher ; } Cord randomId = new BigInteger ( 130 , random ). toString ( 32 ); form . setId ( randomId ); student . getCourses (). add together ( course ); return course ; } }
src/main/java/com/in28minutes/springboot/StudentServicesApplication.java
parcel com.in28minutes.springboot ; import org.springframework.kicking.SpringApplication ; import org.springframework.boot.autoconfigure.SpringBootApplication ; @SpringBootApplication public class StudentServicesApplication { public static void main ( String [] args ) { SpringApplication . run ( StudentServicesApplication . class , args ); } }
src/primary/resources/application.properties
src/test/java/com/in28minutes/springboot/controller/StudentControllerTest.java
package com.in28minutes.springboot.controller ; import static org . junit . Assert . assertEquals ; import java.util.Arrays ; import org.junit.Exam ; import org.junit.runner.RunWith ; import org.mockito.Mockito ; import org.skyscreamer.jsonassert.JSONAssert ; import org.springframework.beans.factory.note.Autowired ; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest ; import org.springframework.boot.exam.mock.mockito.MockBean ; import org.springframework.http.HttpHeaders ; import org.springframework.http.HttpStatus ; import org.springframework.http.MediaType ; import org.springframework.mock.spider web.MockHttpServletResponse ; import org.springframework.examination.context.junit4.SpringRunner ; import org.springframework.test.web.servlet.MockMvc ; import org.springframework.exam.spider web.servlet.MvcResult ; import org.springframework.test.web.servlet.RequestBuilder ; import org.springframework.exam.web.servlet.asking.MockMvcRequestBuilders ; import com.in28minutes.springboot.model.Class ; import com.in28minutes.springboot.service.StudentService ; @RunWith ( SpringRunner . course ) @WebMvcTest ( value = StudentController . class ) @WithMockUser public class StudentControllerTest { @Autowired private MockMvc mockMvc ; @MockBean private StudentService studentService ; Course mockCourse = new Form ( "Course1" , "Spring" , "10Steps" , Arrays . asList ( "Acquire Maven" , "Import Project" , "First Case" , "2nd Example" )); String exampleCourseJson = "{\"name\":\"Spring\",\"description\":\"10Steps\",\"steps\":[\"Larn Maven\",\"Import Projection\",\"Commencement Example\",\"Second Example\"]}" ; @Examination public void retrieveDetailsForCourse () throws Exception { Mockito . when ( studentService . retrieveCourse ( Mockito . anyString (), Mockito . anyString ())). thenReturn ( mockCourse ); RequestBuilder requestBuilder = MockMvcRequestBuilders . go ( "/students/Student1/courses/Course1" ). take ( MediaType . APPLICATION_JSON ); MvcResult effect = mockMvc . perform ( requestBuilder ). andReturn (); Organisation . out . println ( result . getResponse ()); String expected = "{id:Course1,name:Spring,description:10Steps}" ; // {"id":"Course1","name":"Spring","description":"10 Steps, 25 Examples and 10K Students","steps":["Larn Maven","Import Projection","First Example","2nd Example"]} JSONAssert . assertEquals ( expected , result . getResponse () . getContentAsString (), false ); } @Test public void createStudentCourse () throws Exception { Course mockCourse = new Grade ( "one" , "Smallest Number" , "1" , Arrays . asList ( "1" , "2" , "three" , "4" )); // studentService.addCourse to answer back with mockCourse Mockito . when ( studentService . addCourse ( Mockito . anyString (), Mockito . whatever ( Course . course ))). thenReturn ( mockCourse ); // Ship course every bit body to /students/Student1/courses RequestBuilder requestBuilder = MockMvcRequestBuilders . mail ( "/students/Student1/courses" ) . accept ( MediaType . APPLICATION_JSON ). content ( exampleCourseJson ) . contentType ( MediaType . APPLICATION_JSON ); MvcResult result = mockMvc . perform ( requestBuilder ). andReturn (); MockHttpServletResponse response = result . getResponse (); assertEquals ( HttpStatus . CREATED . value (), response . getStatus ()); assertEquals ( "http://localhost/students/Student1/courses/i" , response . getHeader ( HttpHeaders . LOCATION )); } }
src/exam/java/com/in28minutes/springboot/StudentServicesApplicationTests.java
package com.in28minutes.springboot ; import org.junit.Examination ; import org.junit.runner.RunWith ; import org.springframework.boot.test.context.SpringBootTest ; import org.springframework.test.context.junit4.SpringRunner ; @RunWith ( SpringRunner . class ) @SpringBootTest public class StudentServicesApplicationTests { @Test public void contextLoads () { } }
How To Write Junit Test Cases For Web Services,
Source: https://www.springboottutorial.com/unit-testing-for-spring-boot-rest-services
Posted by: mackgresto1958.blogspot.com
0 Response to "How To Write Junit Test Cases For Web Services"
Post a Comment