├── README.md ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── example │ │ ├── DemoApplication.java │ │ ├── exception │ │ ├── ErrorResponse.java │ │ ├── RestExceptionHandler.java │ │ └── ToDoException.java │ │ ├── model │ │ ├── Response.java │ │ └── ToDo.java │ │ ├── repository │ │ └── ToDoRepository.java │ │ ├── service │ │ ├── ToDoService.java │ │ └── ToDoServiceImpl.java │ │ ├── util │ │ └── PayloadValidator.java │ │ └── web │ │ └── ToDoController.java └── resources │ └── application.properties └── test └── java └── com └── example ├── DemoApplicationTests.java ├── service └── ToDoServiceTest.java ├── util └── PayloadValidatorTest.java └── web └── ToDoControllerTest.java /README.md: -------------------------------------------------------------------------------- 1 | # SpringBootUnitTestTutorial 2 | 3 | 1. mvn clean 4 | 2. mvn test 5 | 3. mvn clean install 6 | 4. Go to the target folder 7 | 5. java -jar demo-0.0.1-SNAPSHOT.jar 8 | 6. Verify your RESTful calls. 9 | 10 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.example 7 | demo 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | demo 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.4.2.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-data-jpa 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-web 35 | 36 | 37 | 38 | com.h2database 39 | h2 40 | runtime 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-test 45 | test 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /src/main/java/com/example/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.boot.CommandLineRunner; 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | import org.springframework.context.annotation.Bean; 9 | 10 | import com.example.model.ToDo; 11 | import com.example.repository.ToDoRepository; 12 | 13 | @SpringBootApplication 14 | public class DemoApplication { 15 | 16 | private static final Logger logger = LoggerFactory.getLogger(DemoApplication.class); 17 | 18 | public static void main(String[] args) { 19 | SpringApplication.run(DemoApplication.class, args); 20 | } 21 | 22 | @Bean 23 | public CommandLineRunner setup(ToDoRepository toDoRepository) { 24 | return (args) -> { 25 | toDoRepository.save(new ToDo("Remove unused imports", true)); 26 | toDoRepository.save(new ToDo("Clean the code", true)); 27 | toDoRepository.save(new ToDo("Build the artifacts", false)); 28 | toDoRepository.save(new ToDo("Deploy the jar file", true)); 29 | logger.info("The sample data has been generated"); 30 | }; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/example/exception/ErrorResponse.java: -------------------------------------------------------------------------------- 1 | package com.example.exception; 2 | 3 | public class ErrorResponse { 4 | 5 | private int errorCode; 6 | private String message; 7 | 8 | public int getErrorCode() { 9 | return errorCode; 10 | } 11 | 12 | public void setErrorCode(int errorCode) { 13 | this.errorCode = errorCode; 14 | } 15 | 16 | public String getMessage() { 17 | return message; 18 | } 19 | 20 | public void setMessage(String message) { 21 | this.message = message; 22 | } 23 | } -------------------------------------------------------------------------------- /src/main/java/com/example/exception/RestExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.example.exception; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.http.ResponseEntity; 5 | import org.springframework.web.bind.annotation.ControllerAdvice; 6 | import org.springframework.web.bind.annotation.ExceptionHandler; 7 | 8 | @ControllerAdvice 9 | public class RestExceptionHandler { 10 | 11 | @ExceptionHandler(ToDoException.class) 12 | public ResponseEntity exceptionToDoHandler(Exception ex) { 13 | ErrorResponse error = new ErrorResponse(); 14 | error.setErrorCode(HttpStatus.NOT_FOUND.value()); 15 | error.setMessage(ex.getMessage()); 16 | return new ResponseEntity(error, HttpStatus.NOT_FOUND); 17 | } 18 | 19 | @ExceptionHandler(Exception.class) 20 | public ResponseEntity exceptionHandler(Exception ex) { 21 | ErrorResponse error = new ErrorResponse(); 22 | error.setErrorCode(HttpStatus.BAD_REQUEST.value()); 23 | error.setMessage("The request could not be understood by the server due to malformed syntax."); 24 | return new ResponseEntity(error, HttpStatus.BAD_REQUEST); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/example/exception/ToDoException.java: -------------------------------------------------------------------------------- 1 | package com.example.exception; 2 | 3 | public class ToDoException extends Exception { 4 | 5 | private static final long serialVersionUID = 1L; 6 | private String errorMessage; 7 | 8 | public String getErrorMessage() { 9 | return errorMessage; 10 | } 11 | 12 | public ToDoException(String errorMessage) { 13 | super(errorMessage); 14 | this.errorMessage = errorMessage; 15 | } 16 | 17 | public ToDoException() { 18 | super(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/example/model/Response.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | public class Response { 4 | 5 | private int status; 6 | private String message; 7 | 8 | public Response() { 9 | super(); 10 | } 11 | 12 | public Response(int status, String message) { 13 | super(); 14 | this.status = status; 15 | this.message = message; 16 | } 17 | 18 | public int getStatus() { 19 | return status; 20 | } 21 | 22 | public void setStatus(int status) { 23 | this.status = status; 24 | } 25 | 26 | public String getMessage() { 27 | return message; 28 | } 29 | 30 | public void setMessage(String message) { 31 | this.message = message; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/example/model/ToDo.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.Id; 6 | 7 | @Entity 8 | public class ToDo { 9 | 10 | @Id 11 | @GeneratedValue 12 | private long id; 13 | private String text; 14 | private boolean completed; 15 | 16 | public ToDo() { 17 | super(); 18 | } 19 | 20 | 21 | public ToDo(long id, String text, boolean completed) { 22 | super(); 23 | this.id = id; 24 | this.text = text; 25 | this.completed = completed; 26 | } 27 | 28 | 29 | public ToDo(String text, boolean completed) { 30 | super(); 31 | this.text = text; 32 | this.completed = completed; 33 | } 34 | public long getId() { 35 | return id; 36 | } 37 | public void setId(long id) { 38 | this.id = id; 39 | } 40 | 41 | public String getText() { 42 | return text; 43 | } 44 | public void setText(String text) { 45 | this.text = text; 46 | } 47 | public boolean isCompleted() { 48 | return completed; 49 | } 50 | public void setCompleted(boolean completed) { 51 | this.completed = completed; 52 | } 53 | 54 | 55 | 56 | 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/example/repository/ToDoRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import com.example.model.ToDo; 7 | 8 | @Repository("toDoRepository") 9 | public interface ToDoRepository extends JpaRepository{ 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/example/service/ToDoService.java: -------------------------------------------------------------------------------- 1 | package com.example.service; 2 | 3 | import java.util.List; 4 | 5 | import com.example.model.ToDo; 6 | 7 | public interface ToDoService { 8 | public List getAllToDo(); 9 | public ToDo getToDoById(long id); 10 | public ToDo saveToDo(ToDo todo); 11 | public void removeToDo(ToDo todo); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/example/service/ToDoServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.example.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.example.model.ToDo; 9 | import com.example.repository.ToDoRepository; 10 | 11 | @Service("toDoService") 12 | public class ToDoServiceImpl implements ToDoService{ 13 | 14 | @Autowired 15 | private ToDoRepository toDoRepository; 16 | 17 | @Override 18 | public List getAllToDo() { 19 | return toDoRepository.findAll(); 20 | } 21 | 22 | @Override 23 | public ToDo getToDoById(long id) { 24 | return toDoRepository.findOne(id); 25 | } 26 | 27 | @Override 28 | public ToDo saveToDo(ToDo todo) { 29 | return toDoRepository.save(todo); 30 | } 31 | 32 | @Override 33 | public void removeToDo(ToDo todo) { 34 | toDoRepository.delete(todo); 35 | } 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/example/util/PayloadValidator.java: -------------------------------------------------------------------------------- 1 | package com.example.util; 2 | 3 | import com.example.model.ToDo; 4 | 5 | public class PayloadValidator { 6 | 7 | public static boolean validateCreatePayload(ToDo toDo){ 8 | if (toDo.getId() > 0){ 9 | return false; 10 | } 11 | return true; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/example/web/ToDoController.java: -------------------------------------------------------------------------------- 1 | package com.example.web; 2 | 3 | import java.util.List; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.http.HttpStatus; 9 | import org.springframework.http.ResponseEntity; 10 | import org.springframework.web.bind.annotation.PathVariable; 11 | import org.springframework.web.bind.annotation.RequestBody; 12 | import org.springframework.web.bind.annotation.RequestMapping; 13 | import org.springframework.web.bind.annotation.RequestMethod; 14 | import org.springframework.web.bind.annotation.RestController; 15 | 16 | import com.example.exception.ToDoException; 17 | import com.example.model.Response; 18 | import com.example.model.ToDo; 19 | import com.example.service.ToDoService; 20 | import com.example.util.PayloadValidator; 21 | 22 | @RestController 23 | public class ToDoController { 24 | 25 | private static final Logger logger = LoggerFactory.getLogger(ToDoController.class); 26 | 27 | @Autowired 28 | private ToDoService toDoService; 29 | 30 | @RequestMapping(value="/todo", method=RequestMethod.GET) 31 | public ResponseEntity> getAllToDo(){ 32 | logger.info("Returning all the ToDo´s"); 33 | return new ResponseEntity>(toDoService.getAllToDo(), HttpStatus.OK); 34 | } 35 | 36 | @RequestMapping(value = "/todo/{id}", method = RequestMethod.GET) 37 | public ResponseEntity getToDoById(@PathVariable("id") long id) throws ToDoException{ 38 | logger.info("ToDo id to return " + id); 39 | ToDo toDo = toDoService.getToDoById(id); 40 | if (toDo == null || toDo.getId() <= 0){ 41 | throw new ToDoException("ToDo doesn´t exist"); 42 | } 43 | return new ResponseEntity(toDoService.getToDoById(id), HttpStatus.OK); 44 | } 45 | 46 | @RequestMapping(value = "/todo/{id}", method = RequestMethod.DELETE) 47 | public ResponseEntity removeToDoById(@PathVariable("id") long id) throws ToDoException{ 48 | logger.info("ToDo id to remove " + id); 49 | ToDo toDo = toDoService.getToDoById(id); 50 | if (toDo == null || toDo.getId() <= 0){ 51 | throw new ToDoException("ToDo to delete doesn´t exist"); 52 | } 53 | toDoService.removeToDo(toDo); 54 | return new ResponseEntity(new Response(HttpStatus.OK.value(), "ToDo has been deleted"), HttpStatus.OK); 55 | } 56 | 57 | @RequestMapping(value = "/todo", method = RequestMethod.POST) 58 | public ResponseEntity saveToDo(@RequestBody ToDo payload) throws ToDoException{ 59 | logger.info("Payload to save " + payload); 60 | if (!PayloadValidator.validateCreatePayload(payload)){ 61 | throw new ToDoException("Payload malformed, id must not be defined"); 62 | } 63 | return new ResponseEntity(toDoService.saveToDo(payload), HttpStatus.OK); 64 | } 65 | 66 | @RequestMapping(value = "/todo", method = RequestMethod.PATCH) 67 | public ResponseEntity updateToDo(@RequestBody ToDo payload) throws ToDoException{ 68 | logger.info("Payload to update " + payload); 69 | ToDo toDo = toDoService.getToDoById(payload.getId()); 70 | if (toDo == null || toDo.getId() <= 0){ 71 | throw new ToDoException("ToDo to update doesn´t exist"); 72 | } 73 | return new ResponseEntity(toDoService.saveToDo(payload), HttpStatus.OK); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gustavoponce7/SpringBootUnitTestTutorial/7ee20c7166e19321a26fd2a47a7e118baf004572/src/main/resources/application.properties -------------------------------------------------------------------------------- /src/test/java/com/example/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class DemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/example/service/ToDoServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.example.service; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.mockito.Mockito.times; 5 | import static org.mockito.Mockito.verify; 6 | import static org.mockito.Mockito.when; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | import org.junit.Before; 12 | import org.junit.Test; 13 | import org.junit.runner.RunWith; 14 | import org.mockito.InjectMocks; 15 | import org.mockito.Mock; 16 | import org.mockito.MockitoAnnotations; 17 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 18 | 19 | import com.example.model.ToDo; 20 | import com.example.repository.ToDoRepository; 21 | 22 | @RunWith(SpringJUnit4ClassRunner.class) 23 | public class ToDoServiceTest { 24 | 25 | @Mock 26 | private ToDoRepository toDoRepository; 27 | 28 | @InjectMocks 29 | private ToDoServiceImpl toDoService; 30 | 31 | @Before 32 | public void setup(){ 33 | MockitoAnnotations.initMocks(this); 34 | } 35 | 36 | @Test 37 | public void testGetAllToDo(){ 38 | List toDoList = new ArrayList(); 39 | toDoList.add(new ToDo(1,"Todo Sample 1",true)); 40 | toDoList.add(new ToDo(2,"Todo Sample 2",true)); 41 | toDoList.add(new ToDo(3,"Todo Sample 3",false)); 42 | when(toDoRepository.findAll()).thenReturn(toDoList); 43 | 44 | List result = toDoService.getAllToDo(); 45 | assertEquals(3, result.size()); 46 | } 47 | 48 | @Test 49 | public void testGetToDoById(){ 50 | ToDo toDo = new ToDo(1,"Todo Sample 1",true); 51 | when(toDoRepository.findOne(1L)).thenReturn(toDo); 52 | ToDo result = toDoService.getToDoById(1); 53 | assertEquals(1, result.getId()); 54 | assertEquals("Todo Sample 1", result.getText()); 55 | assertEquals(true, result.isCompleted()); 56 | } 57 | 58 | @Test 59 | public void saveToDo(){ 60 | ToDo toDo = new ToDo(8,"Todo Sample 8",true); 61 | when(toDoRepository.save(toDo)).thenReturn(toDo); 62 | ToDo result = toDoService.saveToDo(toDo); 63 | assertEquals(8, result.getId()); 64 | assertEquals("Todo Sample 8", result.getText()); 65 | assertEquals(true, result.isCompleted()); 66 | } 67 | 68 | @Test 69 | public void removeToDo(){ 70 | ToDo toDo = new ToDo(8,"Todo Sample 8",true); 71 | toDoService.removeToDo(toDo); 72 | verify(toDoRepository, times(1)).delete(toDo); 73 | } 74 | 75 | 76 | 77 | } 78 | 79 | -------------------------------------------------------------------------------- /src/test/java/com/example/util/PayloadValidatorTest.java: -------------------------------------------------------------------------------- 1 | package com.example.util; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.Test; 6 | 7 | import com.example.model.ToDo; 8 | 9 | public class PayloadValidatorTest { 10 | 11 | @Test 12 | public void validatePayLoad() { 13 | ToDo toDo = new ToDo(1, "Sample ToDo 1", true); 14 | assertEquals(false, PayloadValidator.validateCreatePayload(toDo)); 15 | } 16 | 17 | @Test 18 | public void validateInvalidPayLoad() { 19 | ToDo toDo = new ToDo(0, "Sample ToDo 1", true); 20 | assertEquals(true, PayloadValidator.validateCreatePayload(toDo)); 21 | } 22 | 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/com/example/web/ToDoControllerTest.java: -------------------------------------------------------------------------------- 1 | package com.example.web; 2 | 3 | import static org.hamcrest.collection.IsCollectionWithSize.hasSize; 4 | import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; 5 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; 6 | 7 | import org.junit.Before; 8 | import org.junit.FixMethodOrder; 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | import org.junit.runners.MethodSorters; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.boot.test.context.SpringBootTest; 14 | import org.springframework.http.MediaType; 15 | import org.springframework.test.context.ContextConfiguration; 16 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 17 | import org.springframework.test.web.servlet.MockMvc; 18 | import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 19 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 20 | import org.springframework.web.context.WebApplicationContext; 21 | 22 | import com.example.DemoApplication; 23 | 24 | @RunWith(SpringJUnit4ClassRunner.class) 25 | @ContextConfiguration(classes = DemoApplication.class) 26 | @SpringBootTest 27 | @FixMethodOrder(MethodSorters.NAME_ASCENDING) 28 | public class ToDoControllerTest { 29 | 30 | private MockMvc mockMvc; 31 | 32 | @Autowired 33 | private WebApplicationContext wac; 34 | 35 | @Before 36 | public void setup() { 37 | this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); 38 | 39 | } 40 | 41 | @Test 42 | public void verifyAllToDoList() throws Exception { 43 | mockMvc.perform(MockMvcRequestBuilders.get("/todo").accept(MediaType.APPLICATION_JSON)) 44 | .andExpect(jsonPath("$", hasSize(4))).andDo(print()); 45 | } 46 | 47 | @Test 48 | public void verifyToDoById() throws Exception { 49 | mockMvc.perform(MockMvcRequestBuilders.get("/todo/3").accept(MediaType.APPLICATION_JSON)) 50 | .andExpect(jsonPath("$.id").exists()) 51 | .andExpect(jsonPath("$.text").exists()) 52 | .andExpect(jsonPath("$.completed").exists()) 53 | .andExpect(jsonPath("$.id").value(3)) 54 | .andExpect(jsonPath("$.text").value("Build the artifacts")) 55 | .andExpect(jsonPath("$.completed").value(false)) 56 | .andDo(print()); 57 | } 58 | 59 | @Test 60 | public void verifyInvalidToDoArgument() throws Exception { 61 | mockMvc.perform(MockMvcRequestBuilders.get("/todo/f").accept(MediaType.APPLICATION_JSON)) 62 | .andExpect(jsonPath("$.errorCode").value(400)) 63 | .andExpect(jsonPath("$.message").value("The request could not be understood by the server due to malformed syntax.")) 64 | .andDo(print()); 65 | } 66 | 67 | @Test 68 | public void verifyInvalidToDoId() throws Exception { 69 | mockMvc.perform(MockMvcRequestBuilders.get("/todo/0").accept(MediaType.APPLICATION_JSON)) 70 | .andExpect(jsonPath("$.errorCode").value(404)) 71 | .andExpect(jsonPath("$.message").value("ToDo doesn´t exist")) 72 | .andDo(print()); 73 | } 74 | 75 | @Test 76 | public void verifyNullToDo() throws Exception { 77 | mockMvc.perform(MockMvcRequestBuilders.get("/todo/6").accept(MediaType.APPLICATION_JSON)) 78 | .andExpect(jsonPath("$.errorCode").value(404)) 79 | .andExpect(jsonPath("$.message").value("ToDo doesn´t exist")) 80 | .andDo(print()); 81 | } 82 | 83 | @Test 84 | public void verifyDeleteToDo() throws Exception { 85 | mockMvc.perform(MockMvcRequestBuilders.delete("/todo/4").accept(MediaType.APPLICATION_JSON)) 86 | .andExpect(jsonPath("$.status").value(200)) 87 | .andExpect(jsonPath("$.message").value("ToDo has been deleted")) 88 | .andDo(print()); 89 | } 90 | 91 | @Test 92 | public void verifyInvalidToDoIdToDelete() throws Exception { 93 | mockMvc.perform(MockMvcRequestBuilders.delete("/todo/9").accept(MediaType.APPLICATION_JSON)) 94 | .andExpect(jsonPath("$.errorCode").value(404)) 95 | .andExpect(jsonPath("$.message").value("ToDo to delete doesn´t exist")) 96 | .andDo(print()); 97 | } 98 | 99 | 100 | @Test 101 | public void verifySaveToDo() throws Exception { 102 | mockMvc.perform(MockMvcRequestBuilders.post("/todo/") 103 | .contentType(MediaType.APPLICATION_JSON) 104 | .content("{\"text\" : \"New ToDo Sample\", \"completed\" : \"false\" }") 105 | .accept(MediaType.APPLICATION_JSON)) 106 | .andExpect(jsonPath("$.id").exists()) 107 | .andExpect(jsonPath("$.text").exists()) 108 | .andExpect(jsonPath("$.completed").exists()) 109 | .andExpect(jsonPath("$.text").value("New ToDo Sample")) 110 | .andExpect(jsonPath("$.completed").value(false)) 111 | .andDo(print()); 112 | } 113 | 114 | @Test 115 | public void verifyMalformedSaveToDo() throws Exception { 116 | mockMvc.perform(MockMvcRequestBuilders.post("/todo/") 117 | .contentType(MediaType.APPLICATION_JSON) 118 | .content("{ \"id\": \"8\", \"text\" : \"New ToDo Sample\", \"completed\" : \"false\" }") 119 | .accept(MediaType.APPLICATION_JSON)) 120 | .andExpect(jsonPath("$.errorCode").value(404)) 121 | .andExpect(jsonPath("$.message").value("Payload malformed, id must not be defined")) 122 | .andDo(print()); 123 | } 124 | 125 | @Test 126 | public void verifyUpdateToDo() throws Exception { 127 | mockMvc.perform(MockMvcRequestBuilders.patch("/todo/") 128 | .contentType(MediaType.APPLICATION_JSON) 129 | .content("{ \"id\": \"1\", \"text\" : \"New ToDo Text\", \"completed\" : \"false\" }") 130 | .accept(MediaType.APPLICATION_JSON)) 131 | .andExpect(jsonPath("$.id").exists()) 132 | .andExpect(jsonPath("$.text").exists()) 133 | .andExpect(jsonPath("$.completed").exists()) 134 | .andExpect(jsonPath("$.id").value(1)) 135 | .andExpect(jsonPath("$.text").value("New ToDo Text")) 136 | .andExpect(jsonPath("$.completed").value(false)) 137 | .andDo(print()); 138 | } 139 | 140 | @Test 141 | public void verifyInvalidToDoUpdate() throws Exception { 142 | mockMvc.perform(MockMvcRequestBuilders.patch("/todo/") 143 | .contentType(MediaType.APPLICATION_JSON) 144 | .content("{ \"idd\": \"8\", \"text\" : \"New ToDo Sample\", \"completed\" : \"false\" }") 145 | .accept(MediaType.APPLICATION_JSON)) 146 | .andExpect(jsonPath("$.errorCode").value(404)) 147 | .andExpect(jsonPath("$.message").value("ToDo to update doesn´t exist")) 148 | .andDo(print()); 149 | } 150 | 151 | } 152 | --------------------------------------------------------------------------------