{"id":18350036,"url":"https://github.com/eyrafabdullayev/spring-boot-testing","last_synced_at":"2025-04-09T23:49:50.925Z","repository":{"id":152934849,"uuid":"289102825","full_name":"eyrafabdullayev/spring-boot-testing","owner":"eyrafabdullayev","description":"Simple example for Testing in Spring Boot","archived":false,"fork":false,"pushed_at":"2020-08-20T20:38:40.000Z","size":56,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-09T23:49:48.641Z","etag":null,"topics":["integration-testing","java","spring","spring-boot","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-20T20:21:36.000Z","updated_at":"2020-08-20T20:42:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"2e898ac9-2422-4c6c-b557-e81a5c6cc579","html_url":"https://github.com/eyrafabdullayev/spring-boot-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-testing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eyrafabdullayev%2Fspring-boot-testing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eyrafabdullayev%2Fspring-boot-testing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eyrafabdullayev%2Fspring-boot-testing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eyrafabdullayev","download_url":"https://codeload.github.com/eyrafabdullayev/spring-boot-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","spring","spring-boot","testing"],"created_at":"2024-11-05T21:24:59.504Z","updated_at":"2025-04-09T23:49:50.900Z","avatar_url":"https://github.com/eyrafabdullayev.png","language":"Java","readme":"# spring-boot-testing\n\n## About\n\nThe application we're going to use is an API that provides some basic operations on an Employee Resource.\n\n### Dependencies\n\n``` java\n\n\u003cdependency\u003e\n\t  \u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\n\t  \u003cartifactId\u003espring-boot-starter-test\u003c/artifactId\u003e\n\t  \u003cscope\u003etest\u003c/scope\u003e\n\t  \u003cversion\u003e2.2.6.RELEASE\u003c/version\u003e\n\u003c/dependency\u003e\n\u003cdependency\u003e\n\t  \u003cgroupId\u003ecom.h2database\u003c/groupId\u003e\n\t  \u003cartifactId\u003eh2\u003c/artifactId\u003e\n\t  \u003cscope\u003etest\u003c/scope\u003e\n\u003c/dependency\u003e\n\n```\n### Integration Testing With @DataJpaTest\n\nWe're going to create an entity which has id and name as its properties:\n\n``` java\n\n@Entity\n\t@Table(name = \"employee\")\n\tpublic class Employee {\n\t \n\t    @Id\n\t    @GeneratedValue(strategy = GenerationType.AUTO)\n\t    private Long id;\n\t \n\t    @Size(min = 3, max = 45)\n\t    private String name;\n\t \n\t    // standard getters and setters, constructors\n\t}\n  \n```\n\nAnd here is our repository - using Spring Data JPA\n\n``` java\n\n@Repository\n\tpublic interface EmployeeRepository extends JpaRepository\u003cEmployee, Integer\u003e {\n\t \n\t    Employee findByName(String name);\n\t \n\t}\n\n```\n\nAfter that we're going to write our test:\n\n``` java\n\n@RunWith(SpringRunner.class)\n\t@DataJpaTest\n\tpublic class EmployeeRepositoryIntegrationTest {\n\t \n\t    @Autowired\n\t    private TestEntityManager entityManager;\n\t \n\t    @Autowired\n\t    private EmployeeRepository employeeRepository;\n\t \n\t    @Test\n\t    public void whenFindByName_thenReturnEmployee() {\n\t    // given\n\t    Employee james = new Employee(\"James\");\n\t    entityManager.persist(james);\n\t    entityManager.flush();\n\t \n\t    // when\n\t    Employee found = employeeRepository.findByName(james.getName());\n\t \n\t    // then\n\t    assertThat(found.getName())\n\t      .isEqualTo(james.getName());\n\t}\n\t \n\t}\n\n```\n\n\n###  Mocking With @MockBean\n  \nOur Service class is dependent on Repository. So, to test Service layer, we don't need to know about how the persistence layer is implemented:\n\n``` java\n\n@Service\n\tpublic class EmployeeServiceImpl implements EmployeeService {\n\t \n\t    @Autowired\n\t    private EmployeeRepository employeeRepository;\n\t \n\t    @Override\n\t    public Employee getEmployeeByName(String name) {\n\t        return employeeRepository.findByName(name);\n\t    }\n\t}\n\n```\nAnd our test class\n\n``` java\n\n@RunWith(SpringRunner.class)\n\tpublic class EmployeeServiceImplIntegrationTest {\n\t \n\t    @TestConfiguration\n\t    static class EmployeeServiceImplTestContextConfiguration {\n\t \n\t        @Bean\n\t        public EmployeeService employeeService() {\n\t            return new EmployeeServiceImpl();\n\t        }\n\t    }\n\t \n\t    @Autowired\n\t    private EmployeeService employeeService;\n\t \n\t    @MockBean\n\t    private EmployeeRepository employeeRepository;\n\t \n\t    @Before\n\t    public void setUp() {\n\t    Employee alex = new Employee(\"alex\");\n\t \n\t    Mockito.when(employeeRepository.findByName(alex.getName()))\n\t      .thenReturn(alex);\n\t    }\n      \n      @Test\n\t    public void whenValidName_thenEmployeeShouldBeFound() {\n\t    String name = \"alex\";\n\t    Employee found = employeeService.getEmployeeByName(name);\n\t \n\t     assertThat(found.getName())\n\t      .isEqualTo(name);\n\t    }\n\t}\n\n```\nTo check the Service class, we need to have an instance of Service class so that we can @Autowire it in our test class. This configuration is achieved by using the @TestConfiguration annotation.\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feyrafabdullayev%2Fspring-boot-testing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feyrafabdullayev%2Fspring-boot-testing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feyrafabdullayev%2Fspring-boot-testing/lists"}