{"id":18350040,"url":"https://github.com/eyrafabdullayev/spring-boot-rest-testing","last_synced_at":"2026-05-04T21:39:37.586Z","repository":{"id":152934853,"uuid":"289311830","full_name":"eyrafabdullayev/spring-boot-rest-testing","owner":"eyrafabdullayev","description":"Testing of Spring Boot Restful API","archived":false,"fork":false,"pushed_at":"2020-08-21T16:26:14.000Z","size":61,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-09T23:49:50.271Z","etag":null,"topics":["integration-testing","java","jwt","jwt-authentication","spring","spring-boot","spring-security","spring-security-ouath","testing"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eyrafabdullayev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-21T16:09:34.000Z","updated_at":"2020-08-21T16:31:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"819dc1fd-6048-4f88-afa9-b52dc178bd3f","html_url":"https://github.com/eyrafabdullayev/spring-boot-rest-testing","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eyrafabdullayev%2Fspring-boot-rest-testing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eyrafabdullayev%2Fspring-boot-rest-testing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eyrafabdullayev%2Fspring-boot-rest-testing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eyrafabdullayev%2Fspring-boot-rest-testing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eyrafabdullayev","download_url":"https://codeload.github.com/eyrafabdullayev/spring-boot-rest-testing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248131454,"owners_count":21052819,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["integration-testing","java","jwt","jwt-authentication","spring","spring-boot","spring-security","spring-security-ouath","testing"],"created_at":"2024-11-05T21:24:59.857Z","updated_at":"2026-05-04T21:39:37.556Z","avatar_url":"https://github.com/eyrafabdullayev.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# spring-boot-rest-testing\n\n### Unit Testing With @WebMvcTest\n\nOur Controller depends on Service layer, so we are going to include only one method.\n\n``` java\n\t@RestController\n\t@RequestMapping(\"/api\")\n\tpublic class EmployeeRestController {\n\t \n\t    @Autowired\n\t    private EmployeeService employeeService;\n\t \n\t    @GetMapping(\"/employees\")\n\t    public List\u003cEmployee\u003e getAllEmployees() {\n\t        return employeeService.getAllEmployees();\n\t    }\n\t}\n\n```\n\nWe have to mock Service layer for out unit tests.\n\n``` java\n@RunWith(SpringRunner.class)\n\t@WebMvcTest(EmployeeRestController.class)\n\tpublic class EmployeeRestControllerIntegrationTest {\n\t \n\t    @Autowired\n\t    private MockMvc mvc;\n\t \n\t    @MockBean\n\t    private EmployeeService service;\n\t \n\t    // write test cases here\n\t}\n\n```\n\nUsing @WebMvcTest annotation we will control Controllers. It will auto-configure the Spring MVC infrastructure for our unit tests.\n\n``` java\n@Test\n\tpublic void givenEmployees_whenGetEmployees_thenReturnJsonArray()\n\t  throws Exception {\n\t    \n\t    Employee james = new Employee(\"james\");\n\t \n\t    List\u003cEmployee\u003e allEmployees = Arrays.asList(james);\n\t \n\t    given(service.getAllEmployees()).willReturn(allEmployees);\n\t \n\t    mvc.perform(get(\"/api/employees\")\n\t      .contentType(MediaType.APPLICATION_JSON))\n\t      .andExpect(status().isOk())\n\t      .andExpect(jsonPath(\"$\", hasSize(1)))\n\t      .andExpect(jsonPath(\"$[0].name\", is(james.getName())));\n\t}\n\n```\n\n###  Integration Testing With @SpringBootTest\n\nThe integration tests need to start up a container to execute the test cases. Hence, some additional setup is required for this:\n\n``` java\n\n@RunWith(SpringRunner.class)\n@SpringBootTest(\n        webEnvironment = SpringBootTest.WebEnvironment.MOCK,\n        classes = Application.class)\n@AutoConfigureMockMvc\n@TestPropertySource(\n        locations = \"classpath:application.properties\")\npublic class EmployeeRestControllerIntegrationTesting {\n\n    @Autowired\n    private MockMvc mvc;\n\n    @Autowired\n    private EmployeeService employeeService;\n\n    @TestConfiguration\n    static class EmployeeServiceImplTestContextConfiguration {\n\n        @Bean\n        public EmployeeService employeeService() {\n            return new EmployeeServiceImpl();\n        }\n    }\n\n    @MockBean\n    public EmployeeRepository employeeRepository;\n\n    @Test\n    public void givenEmployees_whenGetEmployees_henStatus200() throws Exception {\n\n        Employee employee = new Employee(\"James\");\n\n        List\u003cEmployee\u003e employees = Arrays.asList(employee);\n\n        given(employeeService.getAllEmployees()).willReturn(employees);\n\n        mvc.perform(get(\"/api/v1/employees\")\n           .contentType(MediaType.APPLICATION_JSON))\n           .andExpect(status().isOk())\n           .andExpect(content()\n           .contentTypeCompatibleWith(MediaType.APPLICATION_JSON));\n    }\n}\n\n```\n\nThe @SpringBootTest annotation can be used when we need to bootstrap the entire container. \n\n\u003e Property file loaded with @TestPropertySource will override the existing application.properties file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feyrafabdullayev%2Fspring-boot-rest-testing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feyrafabdullayev%2Fspring-boot-rest-testing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feyrafabdullayev%2Fspring-boot-rest-testing/lists"}