Lab7

pdf

School

University of Toronto *

*We aren’t endorsed by this school

Course

350

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

5

Uploaded by MagistrateWorldChinchilla30

Report
MIE350F Lab #7 Integration Tests in CMS Fall 2023 Objectives: 1. Learn how to implement integration tests in CMS. Before You Begin: 1. Open your CMS project in IntelliJ IDEA. Run your project and make sure the log shows APPLICATION IS RUNNING (Instruction on running IntelliJ and CMS can be found in Lab 1) Important: The database is reset each time you run the application. At any time, you can stop the application and re-start it to re-initialize the database to its original state. 2. Run the Insomnia REST client you have downloaded in lab 2. Example: Integration Test Suite in Spring-based CMS Test suites are collections of automated test cases that are run together. We store all test suites in out project in src/test/java/com.example.cms, where each Java file contains one test suite with one or more test cases. We focus on integration test that focus on testing how multiple components work together in our software (e.g., testing the integration between a controller and the corresponding repository). In IntelliJ, open the file src/test/java/com.example.cms/StudentTests which contain a set of tests that cover the StudentController and the StudentRepository. The class StudentTests has three test cases that test retrieving, adding and deleting a student. Next, we analyze one of the test cases that focuses on adding a student:
@Test void addStudent () throws Exception{ ObjectNode studentJson = objectMapper .createObjectNode() ; studentJson.put( "id" , 8888L ) ; studentJson.put( "firstName" , "first" ) ; studentJson.put( "lastName" , "last" ) ; studentJson.put( "email" , "first@last.com" ) ; MockHttpServletResponse response = mockMvc .perform( post ( "/students" ). contentType( "application/json" ). content(studentJson.toString())) .andReturn().getResponse() ; // assert HTTP code of response is 200 OK assertEquals ( 200 , response.getStatus()) ; // Assert student with id 8888 exists in our repository and then get the student object assertTrue ( studentRepository .findById( 8888L ).isPresent()) ; Student addedStudent = studentRepository .findById( 8888L ).get() ; // Assert the details of the students are correct assertEquals ( 8888L , addedStudent.getId()) ; assertEquals ( "first" , addedStudent.getFirstName()) ; assertEquals ( "last" , addedStudent.getLastName()) ; assertEquals ( "first@last.com" , addedStudent.getEmail()) ; } § The decorator @Test defines a test case. § Next, we use objectMapper to define a new JSON object (named studentJson) that contains the details of a student to be added to CMS. Note: 8888L indicates that the value 8888 is long (and not int). § We use mockMvc that help us test Spring web application by simulating HTTP operations and verifying the responses. Here we send a POST request to the URL “/students” while defining the content type to be JSON and the content to be student JSON object we created earlier. § We use assertEquals to verify that the response HTTP status is 200 (“OK” status). § Next, we use assertTrue and assertEqual to verify that the new student was indeed created in the student repository and that the new student details are the correct ones (i.e., they match the ones we provided in our POST request).
Another test case in this test suite is getStudent: @Test void getStudent () throws Exception{ MockHttpServletResponse response = mockMvc .perform( get ( "/students/1111" )) .andReturn().getResponse() ; assertEquals ( 200 , response.getStatus()) ; ObjectNode receivedJson = objectMapper .readValue(response.getContentAsString() , ObjectNode. class ) ; assertEquals ( 1111L , receivedJson.get( "id" ).longValue()) ; assertEquals ( "Tyrion" , receivedJson.get( "firstName" ).textValue()) ; assertEquals ( "Lannister" , receivedJson.get( "lastName" ).textValue()) ; } Here, we simply perform a GET request for student with ID 1111 and verify the result is what we expect (based on the content of our database). To do so, we use objectMapper to convert the response content to a JSON object. Then we can use the function .get() to get the values in the different fields of the JSON. Note that we need to define the type of data in that field (e.g., we use longValue to extract long values, intValue for int values, and textValue for Strings). Questions to Answer: 1. First, make sure CMS is stopped (not running) before completing the following steps. 2. Run the “StudentTests” test suite: right click on “StudentTests” and select “Run ‘StudentTests’”. a. Let the code run and make sure each of the three test cases were successful by observing the green check next to each of them: Note that you can also see how long each of the test cases took to complete. 3. Next, go to the getStudent test case and change the line: assertEquals ( "Lannister" , receivedJson.get( "lastName" ).textValue()) ; to the line assertEquals ( "Other" , receivedJson.get( "lastName" ).textValue()) ;
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Naturally, now the test case is not expected to run successfully as the expected last name in the assertion (“Other”) will not match the last name returned by CMS for student 1111 (“Lannister”). Run the test suite and observe the result: We can see that now the getStudent() test case has failed. Clicking on it will provide details on the execution of the test case and will indicate the problem that occurred: 4. Our CMS had been extended such that the professor entity now also has a salary field. Professor salary cannot be lower than $30,000 and if there is an attempt to add or update professor details such that they include a salary below 30,000 it will automatically be revised to 30,000 before being saved in the database. Note: this may remind you the question in Quiz 1 (b). a. This salary-related logic had been implemented in the ProfessorController (in functions createProfessor and updateProfessor ). You can use Insomnia to add/update professor details with salary below/above 30,000 and observe the results. b. You are asked to write a test suite that would verify this logic. In particular, implement two test cases in the currently-empty test suite ProfessorSalaryTests : the first test case involves adding a new professor with salary higher than 30,000 and the second test case involves adding a new professor with salary lower than 30,000. In both test cases, after adding the professor you need to retrieve it from the professors repository based on ID and use asserts to verify that:
i. The professor details are correct (match the ones you added) ii. The salary is correct according to the salary logic described above. Hint: the professor repository is already initialized in the test suite in the variable repository. 5. Homework (not graded, but important to complete at home as preparation for project and lab quizzes): a. The code includes another test suite, CourseProfessorTests, that has one test case. Read the test case code and make sure you understand it. Then, answer the following questions: i. Provide a short description of the test case. ii. How many repositories and how many controllers are involved in this test case? iii. Describe the roles of courseJson and addedCourse and the differences between them.