Code Monkey home page Code Monkey logo

readable_tests's Introduction

A sample project showing that tests written with BDDMockito and AssertJ are more readable

Nowadays, most Unit tests are written following the BDD style (Given, When, Then) and use JUnit, Mockito framework and AssertJ assertions, which is shown in section 1.

In section 2, we show how BDDMockito can help to write more readable tests replacing Mockito.when keyword by BDDMockito.given in the "Given" section.

Section 3 improves the code of section 2 using BDDAssertions.then assertions from AssertJ.

1. Usual tests using Mockito.when and Assertions.assertThat from AssertJ

Only Mockito.when keyword is used, but it is used in the "Given" section, which is puzzling :-(

    @Test
    public void retrieveUsersShouldReturnExistingUsersWhenUsersArePresent() {
        // Given
        var user1 = Mockito.mock(User.class);
        var user2 = Mockito.mock(User.class);
        Mockito.when(userRepository.retrieveUsers()).thenReturn(of(user1, user2));

        // When
        var retrievedUsers = userService.retrieveUsers();

        // Then
        Assertions.assertThat(retrievedUsers).containsExactlyInAnyOrder(user1, user2);
    }

See source code

2. Readable tests using BDDMockito.given and Assertions.assertThat from AssertJ

Mockito.when calls have been replaced by BDDMockito.given ones in the "Given" section, which makes more sense and is easier to read :-)

    @Test
    public void retrieveUsersShouldReturnExistingUsersWhenUsersArePresent() {
        // Given
        var user1 = Mockito.mock(User.class);
        var user2 = Mockito.mock(User.class);
        BDDMockito.given(userRepository.retrieveUsers()).willReturn(of(user1, user2));

        // When
        var retrievedUsers = userService.retrieveUsers();

        // Then
        Assertions.assertThat(retrievedUsers).containsExactlyInAnyOrder(user1, user2);
    }

See source code

3. Even more readable tests using BDDMockito.given, Assertions.assertThat and BDDAssertions.then from AssertJ

"Then" sections uses BDDAssertions.then assertions.

Our test now fully complies with BDD style, except of course the lack of when keyword (which is implicit) in the "When" section.

    @Test
    public void retrieveUsersShouldReturnExistingUsersWhenUsersArePresent() {
        // Given
        var user1 = Mockito.mock(User.class);
        var user2 = Mockito.mock(User.class);
        BDDMockito.given(userRepository.retrieveUsers()).willReturn(of(user1, user2));

        // When
        var retrievedUsers = userService.retrieveUsers();

        // Then
        BDDAssertions.then(retrievedUsers)
                .as("Should have returned the list of users")
                .containsExactlyInAnyOrder(user1, user2);
    }

See source code

readable_tests's People

Contributors

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