Code Monkey home page Code Monkey logo

rest_spring_boot_testing's Introduction

Api Rest Springboot with send email with Attachment, documentation in Swagger and Unit Testing in junit


MINI EXAMPLES FRAGMENTS CODE

Task Controller

@Operation(summary = "Update One Task")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "Task Done Changed Successfully",
                    content = { @Content(mediaType = "application/json",
                            schema = @Schema(implementation = Task.class)) }),
            @ApiResponse(responseCode = "404", description = "The Task Not exist or Not Found",
                    content = @Content) })
    @PutMapping("/tasks/{id}")
    public ResponseEntity<Task> updateTaskDone(@PathVariable Long id){
        Task task = taskRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("The task not exists in the id " + id));
        task.setDone(!task.isDone());
        Task taskUpdated = taskRepository.save(task);

        return ResponseEntity.ok(task);
    }

Other EndPoint

@Operation(summary = "Get One Task")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "The Task Found Is",
                    content = { @Content(mediaType = "application/json",
                            schema = @Schema(implementation = Task.class)) }),
            @ApiResponse(responseCode = "500", description = "Internal Server Error",
                    content = @Content) })
    @GetMapping("/tasks/{id}")
    public Optional<Task> getOneTask(@PathVariable Long id){
        try{
            Optional<Task> taskFound = taskRepository.findById(id);
            return taskFound;
        }catch(Exception e){
            return Optional.empty();
        }
    }

You can see the source code for send Email

Unit testing

@Test
void deleteTask() {
    Task testTask = new Task("Test 1", "some test description 1", false);
    taskRepository.save(testTask);
    taskRepository.deleteById(testTask.getId());
    //Search the Task Validating if yet exist
        Optional<Task> taskFound = taskRepository.findById(testTask.getId());
        assertEquals(Optional.empty(),taskFound);
    }

Other test

    boolean changeDoneTask (Long id){
        Task task = taskRepository.findById(id)
                .orElseThrow(() -> new ResourceNotFoundException("The task not exists in the id " + id));
        boolean stateStarted = task.isDone();
        task.setDone(!task.isDone());
        Task updatedTask = taskRepository.save(task);

        if(stateStarted != updatedTask.isDone()){
            return true;
        }else{
            return false;
        }
    }

    @Test
    void updateTaskDone() {
        Task testTask = new Task("Test 1", "some test description 1", false);
        taskRepository.save(testTask);
        assertTrue(changeDoneTask(testTask.getId()));
    }

Result Of Tests

rest_spring_boot_testing's People

Contributors

johngualteros avatar

Stargazers

 avatar  avatar  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.