Skip to content

Improve test code (one test per method, ParameterizedTests) #676

@koppor

Description

@koppor

(META: This issue is reserved for a university course. Please only work on it if it is part of your assignment)

Context

In unit tests, there should be one assertion per test. An anti-pattern is to include multiple, un-related assertions in one test method. For instance, this is the case at [AuthorListTest#fixAuthorLastNameFirstCommasOxfordComma]https://github.com/JabRef/jabref/blob/1ebe80224500556e3b5a2076f5d388671a59dd8d/src/test/java/org/jabref/model/entry/AuthorListTest.java#L338).

A good practice is to have Parameterized Tests instead of mass code duplication. For instance,

        assertEquals("Smith", AuthorList.fixAuthorLastNameOnlyCommas("John Smith", false));
        assertEquals("Smith", AuthorList.fixAuthorLastNameOnlyCommas("Smith, Jr, John", false));

can be replaced by

    @CsvSource(
            textblock = """
                    Smith; John Smith;false
                    Smith; Smith, Jr, John;false
            """", delimiter = ';')
    @ParameterizedTest
    public void fixAuthorLastNameOnlyCommasNew(String expected, String input, boolean oxfordComma) {
        assertEquals(expected, AuthorList.fixAuthorLastNameOnlyCommas(input, oxfordComma));
    }

Update also read at https://devdocs.jabref.org/code-howtos/testing.html

One can also use @MethodSource if issues with @CsvSource are encountered.

The code

        assertEquals("", AuthorList.fixAuthorLastNameOnlyCommas("", false));

Cannot be covered using CsvSource, because the empty string in CsvSource is reduced to null by JUnit. Thus, keep that test separatere, and only test the non-empty string with CsvSource.

Tasks

  • Rewrite fixAuthorLastNameOnlyCommas using @CsvSource and @ParameterizedTest
  • Split AuthorListTest#getAuthor into three test methods
  • Rewrite AuthorListTest#removeStartAndEndBraces using @CsvSource and @ParameterizedTest
  • Try to split or rewrite other test methods in AuthorListTest

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions