Refactor simple Unit Tests#7571
Conversation
It is unnecessary to create a new formatter for each test.
("test smell": General Fixture / Test Code Duplication)
renamed UnicodeConverterTest to UnicodeConverterFormatterTest
It is unnecessary to create a new formatter for each test.
("test smell": General Fixture / Test Code Duplication)
It is unnecessary to create a new formatter for each test.
("test smell": General Fixture / Test Code Duplication)
It is unnecessary to create a new comparator before each test and setting it to null after each test.
("test smell": General Fixture / Test Code Duplication)
test code quality improvement
("test smell": Test Code Duplication)
easier to maintain, less risk of error propagation.
| public void tearDown() { | ||
| comparator = null; | ||
| } | ||
| private static final CrossRefEntryComparator comparator = new CrossRefEntryComparator(); |
There was a problem hiding this comment.
Why did you change this? I think it's good practice to initialize the object under test in a setup method, so that e.g. changes to it in one test doesn't leak to other tests.
There was a problem hiding this comment.
If I see it correctly it is not possible to change the CrossRefEntryComparator Object after creation. In other words there are no setter methods and the constructor does not take any arguments.
That is why I think the beforeeach is superfluous. Why create a new object, if the one created can be used?
On the other hand, I get your point. If changes to this class are done, like adding arguments to the constructor or adding setter methods, then the BeforeEach makes sense.
There was a problem hiding this comment.
Creation of objects I java is extremely efficient. This shouldn't be a concern.
There was a problem hiding this comment.
In tests, one just does not use static.
- Possiblity one: class variable (you can go for that, because the
setupjust does that) --> move the initialization code up. - Possibility two: use
setup(as in the code)
There was a problem hiding this comment.
Reason for no static: Each test has to be independed of the last test. Thus, everything under test has to be in the initial state.
There was a problem hiding this comment.
Thanks for the feedback! I'll revert these changes then and use the setup method for those formatter tests.
|
It's nice that you are interested in contributing to JabRef, however, it would be nice if you not only focused on simple technical issues, but on issues that affect the users. |
koppor
left a comment
There was a problem hiding this comment.
Some small comments, then it should be good to go.
| public void tearDown() { | ||
| comparator = null; | ||
| } | ||
| private static final CrossRefEntryComparator comparator = new CrossRefEntryComparator(); |
There was a problem hiding this comment.
In tests, one just does not use static.
- Possiblity one: class variable (you can go for that, because the
setupjust does that) --> move the initialization code up. - Possibility two: use
setup(as in the code)
| assertCaseChangerTitleLowers("Hallo world- how", "HAllo WORLD- HOW"); | ||
| private static Stream<Arguments> provideStringsForTitleLowers() { | ||
| return Stream.of( | ||
| // part1 |
There was a problem hiding this comment.
Please try to summarize the part to a more speaking name (or just delete the comment and use empty lines as separator between the blocks)
|
|
||
| private void assertCaseChangerAllLowers(final String string, final String string2) { | ||
| assertEquals(string, BibtexCaseChanger.changeCase(string2, FORMAT_MODE.ALL_LOWERS)); | ||
| /* the real test would look like as follows. Also from the comment of issue 176, order reversed as the "should be" comes first |
There was a problem hiding this comment.
Please add @Disabled and do not work with commented code.
| ); | ||
| } | ||
|
|
||
| private void assertPurify(final String string, final String string2) { |
There was a problem hiding this comment.
Please integrate in testPurify. We don't see why this private (!) method is there.
| new ProtectedTermsLoader(new ProtectedTermsPreferences(ProtectedTermsLoader.getInternalLists(), | ||
| Collections.emptyList(), Collections.emptyList(), Collections.emptyList()))); | ||
| } | ||
| private static final ProtectTermsFormatter formatter = new ProtectTermsFormatter( |
There was a problem hiding this comment.
| private static final ProtectTermsFormatter formatter = new ProtectTermsFormatter( | |
| private ProtectTermsFormatter formatter = new ProtectTermsFormatter( |
| public void setUp() { | ||
| formatter = new LowerCaseFormatter(); | ||
| } | ||
| private static final LowerCaseFormatter formatter = new LowerCaseFormatter(); |
There was a problem hiding this comment.
| private static final LowerCaseFormatter formatter = new LowerCaseFormatter(); | |
| private LowerCaseFormatter formatter = new LowerCaseFormatter(); |
| public void setUp() { | ||
| formatter = new CapitalizeFormatter(); | ||
| } | ||
| private static final CapitalizeFormatter formatter = new CapitalizeFormatter(); |
There was a problem hiding this comment.
| private static final CapitalizeFormatter formatter = new CapitalizeFormatter(); | |
| private CapitalizeFormatter formatter = new CapitalizeFormatter(); |
| public void setUp() { | ||
| formatter = new UnitsToLatexFormatter(); | ||
| } | ||
| private static final UnitsToLatexFormatter formatter = new UnitsToLatexFormatter(); |
There was a problem hiding this comment.
| private static final UnitsToLatexFormatter formatter = new UnitsToLatexFormatter(); | |
| private UnitsToLatexFormatter formatter = new UnitsToLatexFormatter(); |
| void setUp() { | ||
| formatter = new UnicodeToLatexFormatter(); | ||
| } | ||
| private static final UnicodeToLatexFormatter formatter = new UnicodeToLatexFormatter(); |
There was a problem hiding this comment.
| private static final UnicodeToLatexFormatter formatter = new UnicodeToLatexFormatter(); | |
| private UnicodeToLatexFormatter formatter = new UnicodeToLatexFormatter(); |
- remove unmeaningful comment - testSpecialBracketPlacement - add disabled for commented code - integrate assertion in testPurify
Thanks for the comments and suggested changes. I addressed them in the last commits. |
| @Disabled | ||
| @Test | ||
| public void testTitleCaseAllUppers() { | ||
| /* the real test would look like as follows. Also from the comment of issue 176, order reversed as the "should be" comes first */ | ||
| // assertCaseChangerTitleUppers("This is a Simple Example {TITLE}", "This is a simple example {TITLE}"); | ||
| // assertCaseChangerTitleUppers("This {IS} Another Simple Example Tit{LE}", "This {IS} another simple example tit{LE}"); | ||
| // assertCaseChangerTitleUppers("{What ABOUT thIS} one?", "{What ABOUT thIS} one?"); | ||
| // assertCaseChangerTitleUppers("{And {thIS} might {a{lso}} be possible}", "{And {thIS} might {a{lso}} be possible}") |
There was a problem hiding this comment.
In case a test is @Disabled, the contents of the test can go enabled. Since this is probably the last comment on this PR, I'll let it go and we will fix for ourselves.
* 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) ...
I have refactored some simple unit tests to make them easier to understand, more readable, more maintainable and less likely for human error. Some identified test smells (like General Fixture and Test Code Duplication) have been fixed.
It addresses #6207, although it is mainly a refactoring.
CHANGELOG.mddescribed in a way that is understandable for the average user (if applicable)