Git Product home page Git Product logo

employees-api's Introduction

Employees Api

Simple api made with Spring Boot 3 using MySQL for the database.

Persistence Tests

@SpringBootTest
@DisplayName("Employees Repository Integration Tests")
public class EmployeesTests extends MySQLTestBase {
    @Autowired
    private EmployeeRepository repository;

    @AfterEach
    public void cleanup() {
        repository.deleteAll();
    }

    @Test
    @DisplayName("Create Employee")
    public void createEmployeeTest() {

        var newEntity = Employee.builder()
                .firstname("John")
                .lastname("Doe").build();

        var createdEntity = repository.saveAndFlush(newEntity);

        assertTrue(createdEntity.getId() > 0L);
        assertEquals(newEntity.getFirstname(), createdEntity.getFirstname());
        assertEquals(newEntity.getLastname(), createdEntity.getLastname());
    }
}

Integration Tests

@SpringBootTest
@AutoConfigureMockMvc
public class CreateEmployeeTests extends MySQLTestBase {

    @Autowired
    private MockMvc mockMvc;
    @Autowired
    private ObjectMapper mapper;
    @Autowired
    private EmployeeRepository repository;
    @AfterEach
    public void cleanup() {
        repository.deleteAll();
    }
    @Test
    public void createEmployeeStatusShouldBeOk() throws Exception {

        var createdDto = new CreateEmployeeDto("John", "Doe");

        var request = MockMvcRequestBuilders
                .post("/employees")
                .accept(MediaType.APPLICATION_JSON)
                .contentType(MediaType.APPLICATION_JSON)
                .content(mapper.writeValueAsString(createdDto));

        mockMvc.perform(request)
                .andExpect(status().isOk())
                .andExpect(jsonPath("$", Matchers.notNullValue()))
                .andExpect(jsonPath("$.firstname").value("John"))
                .andExpect(jsonPath("$.lastname").value("Doe"));
    }

    @Test
    public void createEmployeeWhenValidationErrorStatusShouldBe400() throws Exception{
        var createdDto = new CreateEmployeeDto("", "Doe");

        var request = MockMvcRequestBuilders
                .post("/employees")
                .accept(MediaType.APPLICATION_JSON)
                .contentType(MediaType.APPLICATION_JSON)
                .content(mapper.writeValueAsString(createdDto));

        mockMvc.perform(request)
                .andExpect(status().isBadRequest());
    }

}

Libraries

  • JPA
  • Test Containers

employees-api's People

Contributors

juan-canseco-dev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.