koppor / jabref

Collection of simple for JabRef issues. Please submit PRs to https://github.com/jabRef/jabref/.
https://github.com/jabRef/jabref/
MIT License
8 stars 14 forks source link

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

Open koppor opened 4 months ago

koppor commented 4 months ago

(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(
            value = {
                    "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));
    }

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

Watson15 commented 4 months ago

How would I test to make sure just my additional tests work instead of having to run ./gradlew test which runs all the tests?

koppor commented 4 months ago

On the guidelines to setup a workspace. At the bottom. There was an optional section. Please follow the steps there

https://devdocs.jabref.org/getting-into-the-code/guidelines-for-setting-up-a-local-workspace/intellij-12-build.html