Skip to content

test: improve test locality #188

@nicktorwald

Description

@nicktorwald

At least, we need to increase test locality moving arrange-act-assert pattern within one test method or at most in one test class for the reused arrangements (@ before / @ after).

An example of a test class structure:

@DisplayName("A resultSet")
class SelectOperationIT {
    
    // text fixture
    Statement statement;
    ...

    // reused for each test preparations
    @BeforeEach
    public void setUp() throws Exception {
        statement.executeUpdate("CREATE TABLE test (id INT PRIMARY KEY, val VARCHAR(50))");
    }

    // cleanup garbage after the test execution
    @AfterEach
    public void tearDown() throws Exception {
        statement.executeUpdate("DROP TABLE test");
    }

    @Test
    @DisplayName("fetched one row by id = 1")
    public void testOneRowSelect() {
        // arrange (given)
        String insertQuery = "INSERT INTO test VALUES (1, 'a');
        statement.executeUpdate(insertQuery);
        
        // act (when)
        String selectQuery = "SELECT * FROM test WHERE id = 1";	
        ResultSet resultSet = statement.executeUpdate(selectQuery);
        resultSet.next();
    
        // assert (then)
        int expectedResultId = 1;
        String expectedResultValue = "a";
        assertEquals(expectedResultId, result.getInt(1));
        assertEquals(expectedResultValue, result.getString(2));
    }

     ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    code healthImprove code readability, simplify maintenance and so on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions