Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple Unit Tests #7542

Merged
merged 20 commits into from
Mar 25, 2021
Merged

Add simple Unit Tests #7542

merged 20 commits into from
Mar 25, 2021

Conversation

Davfon
Copy link
Contributor

@Davfon Davfon commented Mar 15, 2021

I have added some simple unit tests that will increase code coverage / branch coverage.
They contribute to issue #6207

  • Change in CHANGELOG.md described in a way that is understandable for the average user (if applicable)
  • Tests created for changes (if applicable)
  • Manually tested changed features in running JabRef (always required)
  • Screenshots added in PR description (for UI changes)
  • Checked documentation: Is the information available and up to date? If not created an issue at https://github.com/JabRef/user-documentation/issues or, even better, submitted a pull request to the documentation repository.

@calixtus
Copy link
Member

Hey @Davfon , great to see someone takes a look at our test suite!
At first glance, the tests themselves look good. But I think, you could also convert these tests to parameterized tests (there are some examples in our test suite) if you like. It's pretty easy. You could also use the builder pattern for the creation of bibentries for testing (new BibEntry().withField()...)

@Davfon
Copy link
Contributor Author

Davfon commented Mar 16, 2021

Hi there @calixtus !

Thanks for the quick feedback! I willl look into it and will try to convert them to parameterized tests wherever possible.

@@ -151,4 +153,163 @@ public void parseCorrectlyByShortNameGermanLowercase() {
assertEquals(Optional.of(Month.NOVEMBER), Month.parse("nov"));
assertEquals(Optional.of(Month.DECEMBER), Month.parse("dez"));
}

@Test
public void parseGermanShortMonthTest() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there more potential for parameterized tests here?

Copy link
Contributor Author

@Davfon Davfon Mar 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there is. Thanks for the hint. I wasn't aware that you can also pass the expected result to the test.
Really nice feature :D
I changed all of them to parameterized Tests now.


@Test
public void hashCodeTest() {
assertEquals(Objects.hash(path, 10, 1, 4, "lineText"), citation.hashCode());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests for Hashcode and toString do not really add any value and especially toString is more of an internal representation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok, seemed like a rather easy way to increase the code coverage. Thanks for letting me know, I'll try to avoid them in them in the future. (I removed them with commit 21050ff)

@@ -110,7 +110,7 @@
* @return the corresponding month instance, empty if input is not in German
* form
*/
private static Optional<Month> parseGermanShortMonth(String value) {
public static Optional<Month> parseGermanShortMonth(String value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just remove the public prefix and make it "default"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand it correctly, you mean like this: 5022f10?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this makes it package visible, so it can be accessed by the unit test but not from others https://www.baeldung.com/java-access-modifiers

assertEquals(Collections.emptyList(), checker.check(entry));
}

@Test
void KeyFromAuthorAndTitle() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I detect that this code is problematic. According to the Bad practice (BAD_PRACTICE), Nm: Method names should start with a lower case letter (NM_METHOD_NAMING_CONVENTION).
Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! Changed it in commit 93ab8a4

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to help.

public void equalsTest() {
Citation citation1 = new Citation(path, 10, 1, 4, "lineText");
Citation citation2 = null;
assertTrue(citation.equals(citation1));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not make any sense, why don't you use assertEquals? assertEquals calls the equals of the underlying objects. There is also a assertNotEquals method

Copy link
Contributor Author

@Davfon Davfon Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that one is on me. Thanks for the feedback! :) Fixed in commit 6195973

Copy link
Member

@Siedlerchr Siedlerchr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some minor issue, rest is okay

@Siedlerchr Siedlerchr added the status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers label Mar 25, 2021
Copy link
Member

@calixtus calixtus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me too, thanks! Merging now.

@calixtus calixtus merged commit 49b9e3c into JabRef:master Mar 25, 2021
Siedlerchr added a commit that referenced this pull request Mar 28, 2021
* upstream/master: (191 commits)
  Fix for issue 7416: font size of the preferences dialog does not update with the rest of the GUI. (#7509)
  Fix school/instituation is printed twice (#7574)
  Dsiable notarisation until we hae an account for JabRef e.V. (#7572)
  Fix citation keys unintentionally being overwritten on import (#7443)
  Fix AuthentificationPlugin not declared in mergedModule (#7570)
  Suggestions for changes in caching latex free authors (#7301)
  Add simple Unit Tests (#7542)
  Fix drag and drop into empty library (#7555)
  Bump richtextfx from 0.10.4 to 0.10.6 (#7563)
  Bump pdfbox from 2.0.22 to 2.0.23 (#7561)
  Bump org.eclipse.jgit (#7560)
  Bump fontbox from 2.0.22 to 2.0.23 (#7562)
  Bump guava from 30.1-jre to 30.1.1-jre (#7564)
  Bump xmpbox from 2.0.22 to 2.0.23 (#7565)
  Bump hmarr/auto-approve-action from v2.0.0 to v2.1.0 (#7566)
  Add gource (#7193)
  UI: Fix for group icon (#7552)
  Fix for issue 6487: Opening BibTex file (doubleclick) from Folder with spaces not working (#7551)
  add ability to insert arxivId (#7549)
  Fixed missing trigger for linked file operations (#7548)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants