Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barcellos-pedro committed Feb 1, 2024
1 parent ad81cd4 commit d2cf76e
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 9 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ Project based on [Spring Guide](https://spring.io/guides/tutorials/rest/)
- Refactor (Project Structure)
- [x] Controller
- [x] Model
- [x] Interface, etc
- [x] Interface

- [x] [Metrics and Tracing](https://spring.io/guides/gs/tanzu-observability/)
- [x] [Dockerfile](https://spring.io/guides/topicals/spring-boot-docker/)


### Todo
- [Tests](https://docs.spring.io/spring-boot/docs/3.2.2/reference/html/features.html#features.testing)
- [x] [Json](https://spring.academy/courses/building-a-rest-api-with-spring-boot)
- [ ] Unit
- [x] [JSON](https://spring.academy/courses/building-a-rest-api-with-spring-boot)
- [ ] Unit (in progress)
- [ ] Integration (in progress)
- [ ] [WebMvc](https://spring.io/guides/gs/testing-web/)


Expand All @@ -24,11 +25,10 @@ Project based on [Spring Guide](https://spring.io/guides/tutorials/rest/)
- [x] Test
- [ ] Publish API Docs

### Bônus 🎁

- [ ] Postgres + PGAdmin using Docker

- [ ] [API Docs with Restdocs](https://spring.io/guides/gs/testing-restdocs/)
- [ ] Replace H2 to Postgres
- [ ] Monitor Database with PGAdmin using Docker
- [ ] [Caching (Redis)](https://docs.spring.io/spring-framework/reference/integration/cache/annotations.html)

- [ ] Docker Compose

- [ ] [Restdocs (Documentation)](https://spring.io/guides/gs/testing-restdocs/)
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.pedro.rest.service.impl;

import com.pedro.rest.assembler.EmployeeModelAssembler;
import com.pedro.rest.model.Employee;
import com.pedro.rest.repository.EmployeeRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.verify;

@SpringBootTest
public class EmployeeServiceIntegrationTest {
@Autowired
private EmployeeServiceImpl service;

@MockBean
private EmployeeModelAssembler assembler;

@MockBean
private EmployeeRepository repository;

private CollectionModel<EntityModel<Employee>> entityModels;

@BeforeEach
void setup() {
Employee employee = new Employee("Pedro", "Reis", "developer");
EntityModel<Employee> entity = EntityModel.of(employee);
entityModels = CollectionModel.of(List.of(entity));
}

@Test
void getAll() {
when(service.getAll()).thenReturn(entityModels);

var result = service.getAll();
var size = result.getContent().size();

assertEquals(1, size);
verify(repository, atLeast(1)).findAll();
verify(assembler).toCollectionModel(anyCollection());
}
}
52 changes: 52 additions & 0 deletions src/test/java/com/pedro/rest/service/impl/EmployeeServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.pedro.rest.service.impl;

import com.pedro.rest.assembler.EmployeeModelAssembler;
import com.pedro.rest.model.Employee;
import com.pedro.rest.repository.EmployeeRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
public class EmployeeServiceTest {

@InjectMocks
private EmployeeServiceImpl service;

@Mock
private EmployeeModelAssembler assembler;

@Mock
private EmployeeRepository repository;

private CollectionModel<EntityModel<Employee>> entityModels;

@BeforeEach
void setup() {
Employee employee = new Employee("Pedro", "Reis", "developer");
EntityModel<Employee> entity = EntityModel.of(employee);
entityModels = CollectionModel.of(List.of(entity));
}

@Test
void getAll() {
when(service.getAll()).thenReturn(entityModels);

var result = service.getAll();
var size = result.getContent().size();

assertEquals(1, size);
verify(repository, atLeast(1)).findAll();
verify(assembler).toCollectionModel(anyCollection());
}
}

0 comments on commit d2cf76e

Please sign in to comment.