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

Refactoring and addition of unit tests #7581

Merged
merged 28 commits into from
May 6, 2021
Merged

Refactoring and addition of unit tests #7581

merged 28 commits into from
May 6, 2021

Conversation

BShaq
Copy link
Contributor

@BShaq BShaq commented Mar 28, 2021

This pull request includes new unit tests for three classes which increases their line/statement coverage.
They contribute to issue #6207.

Further, I have also refactored existing unit tests to make them more readable, understandable and maintanable.
In this sense, I focused more on test smells described in the paper Refactoring test code (van Deursen et al.) and also on the Code Howtos (Test Cases) in the Development Documentation.

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

BShaq added 20 commits March 24, 2021 13:53
Added assertion explanation to give an explanatory message when the assertion fails.
Additionally removed the setup() method, since it was only used by a few test cases
Tests which exercise more than one production method are harder to read and understand, thus difficult to use as documentation. By splitting the two refactored tests into multiple ones, the readability has hugely increased.
… Eager Test)

To not introduce another Test Smell (Sensitive Equality), I used the equals() method to compare the strings.
…Tests)

Additionally removed the 'test' prefix in the naming as suggested in the 'Code Howtos' of the contribution guidelines.
…nsitive Equality)

In addition, I removed the 'test' prefix in the namings as suggested in the contribution guidelines.
Added description for each assertion, which makes it easier to detect which one failed (in the case when the test itself fails)
Created new unit tests for each production method.
Increase of Line/Statement coverage
Increase of Line/Statement coverage
Increase of Line/Statement coverage
Copy link
Member

@koppor koppor left a comment

Choose a reason for hiding this comment

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

Some comments, especially on using assertEquals(Optional.of(...), ...) instead of assertTrue(...isPresent()

@Test
public void sameAuthor() {
String expectedAuthor = "Jaroslav Kucha ˇr";
assertTrue(annotationViewModel.getAuthor().equals(expectedAuthor));
Copy link
Member

Choose a reason for hiding this comment

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

Why not assertEquals? This is the usual way to check .equals.

Comment on lines 39 to 40
String expectedPage = "1";
assertTrue(annotationViewModel.getPage().equals(expectedPage));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
String expectedPage = "1";
assertTrue(annotationViewModel.getPage().equals(expectedPage));
assertEquals("1", annotationViewModel.getPage());

@Test
public void retrieveCorrectDateAsString() {
String expectedDate = "2017-07-20 10:11:30";
assertTrue(annotationViewModel.getDate().equals(expectedDate));
Copy link
Member

Choose a reason for hiding this comment

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

Please create a one-liner like shown above.

@Test
public void retrieveCorrectContent() {
String expectedContent = "This is content";
assertTrue(annotationViewModel.getContent().equals(expectedContent));
Copy link
Member

Choose a reason for hiding this comment

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

Please create a one-liner like shown above.


@Test
void compareAuthorFieldBiggerAscending() throws Exception {

Copy link
Member

Choose a reason for hiding this comment

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

No starting empty line please

Comment on lines 13 to 21
private BibEntry entry1;
private BibEntry entry2;

@BeforeEach
void setup() {
entry1 = new BibEntry();
entry2 = new BibEntry();
}

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
private BibEntry entry1;
private BibEntry entry2;
@BeforeEach
void setup() {
entry1 = new BibEntry();
entry2 = new BibEntry();
}
private BibEntry entry1 = new BibEntry();
private BibEntry entry2 = new BibEntry();

}

private EditionChecker createBibtexEditionChecker(Boolean bool) {
Copy link
Member

Choose a reason for hiding this comment

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

Please provide a speaking name for bool

BibEntry entry2 = new BibEntry();
entry2.setField(StandardField.OWNER, initialOwner);

assertTrue(entry.getField(StandardField.OWNER).isPresent(), "Owner field for entry is not present");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assertTrue(entry.getField(StandardField.OWNER).isPresent(), "Owner field for entry is not present");
assertEquals(Optional.of(initialOwner), entry.getField(StandardField.OWNER));

Similar for all other Optional tests. We should avoid assertTrue in all cases; we should use assertEquals wherevery possible.

TimestampPreferences timestampPreferences = createTimestampPreference();
UpdateField.setAutomaticFields(bibs, false, ownerPreferences, timestampPreferences);

assertEquals(entry.getField(StandardField.OWNER).get(), initialOwner, "entry has new value for owner field");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assertEquals(entry.getField(StandardField.OWNER).get(), initialOwner, "entry has new value for owner field");
assertEquals(Optional.of(initalOwner), entry.getField(StandardField.OWNER));

@koppor koppor added the status: changes required Pull requests that are not yet complete label Apr 12, 2021
@BShaq
Copy link
Contributor Author

BShaq commented Apr 26, 2021

Hi @koppor
Thanks for reviewing!

I finally adjusted the code accoring to your comments and suggestions :)

public void testCreateTextBold() {
String testText = "this is a test text";
public void stringRemainsTheSameAfterTransformationToNormal() {
Text text = TooltipTextUtil.createText(testText, TooltipTextUtil.TextType.NORMAL);
Copy link
Member

Choose a reason for hiding this comment

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

Not addressed. Not that important, but this

        String textStyle = "Regular";

        assertEquals(textStyle, text.getFont().getStyle());

is IMHO less readable than

        assertEquals("Regular", text.getFont().getStyle());

@koppor koppor merged commit 65874f5 into JabRef:main May 6, 2021
@koppor koppor removed the status: changes required Pull requests that are not yet complete label May 6, 2021
Siedlerchr added a commit that referenced this pull request May 15, 2021
* upstream/main: (71 commits)
  [Bot] Update CSL styles (#7735)
  Fix for issue 6966: open all files of multiple entries (#7709)
  Add simple unit tests (#7696)
  Add simple unit tests (#7543)
  Update check-outdated-dependencies.yml
  Added preset for new entry keybindings and reintroduced defaults (#7705)
  Select the entry which has smaller dictonary order when merge (#7708)
  Update CHANGELOG.md
  fix: make more fields, fomatters, ids and languages sorted by alphabetical order (#7717)
  Bump libreoffice from 7.1.2 to 7.1.3 (#7721)
  Bump unoloader from 7.1.2 to 7.1.3 (#7724)
  Bump org.beryx.jlink from 2.23.7 to 2.23.8 (#7723)
  Bump org.openjfx.javafxplugin from 0.0.9 to 0.0.10 (#7725)
  fix: make fields sorted by lexicographical order (#7711)
  Fix tests
  Refactoring existing unit tests (#7687)
  Refactoring and addition of unit tests (#7581)
  Refactor simple Unit Tests (#7571)
  Add simple unit tests (#7544)
  add and extend unit tests (#7685)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants