Giter VIP home page Giter VIP logo

todo-springboot's Introduction

todo-springboot

todo-testing

Service Test

public class TaskServiceTest {

    @Mock
    TodoValidator todoValidator;

    @Mock
    TodoRepo todoRepository;

    private TodoService taskService;

    @Captor
    ArgumentCaptor<TodoItem> todoItemArgumentCaptor;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.openMocks(this);
        taskService = new TodoService(todoValidator, todoRepository);
    }



    @Test
    void itShouldAddNewTodoTask() {
        // given
        String title = "Interview Test";
        TodoItem todoItem = new TodoItem(title, LocalDate.now(), "some desription", Status.PENDING);

        given(todoRepository.findByTitle(title)).willReturn(Optional.empty());
        given(todoValidator.validateTitleLength(title)).willReturn(true);

        // when
        taskService.addTask(todoItem);

        // then
        then(todoRepository).should().save(todoItemArgumentCaptor.capture());
        TodoItem todoItemCaptor = todoItemArgumentCaptor.getValue();

        assertThat(todoItemCaptor).isEqualTo(todoItem);
    }
}


    @BeforeEach
    public void setup() {
        // We would need this line if we would not use the MockitoExtension
        // MockitoAnnotations.initMocks(this);
        // Here we can't use @AutoConfigureJsonTesters because there isn't a Spring context
        JacksonTester.initFields(this, new ObjectMapper());
        // MockMvc standalone approach
        mockMvc = MockMvcBuilders.standaloneSetup(testController)
                .build();
    }

Controller Test

@ExtendWith( SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc
public class TestControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void getsAllEntries() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/test")
                        .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andReturn();
    }

}

// for post
String json = TestUtils.asJsonString(entity);

mockMvc.perform(post("/test")
    .contentType(MediaType.APPLICATION_JSON)
    .content(json)
    .characterEncoding("utf-8"))
.andExpect(status().isOk())
.andReturn();

Utils class for object to json string converter

public class TestUtils {
    public static String asJsonString(final Object obj) {
        try {
            final ObjectMapper mapper = new ObjectMapper();
            final String jsonContent = mapper.writeValueAsString(obj);
            return jsonContent;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

todo-springboot's People

Contributors

kidusmt avatar

Stargazers

SamsonGT avatar Roman avatar

Watchers

James Cloos avatar  avatar

Forkers

samsongulma

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.