You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We notice that you use the loop structure in your test cases.
For example, testInvalidChar() in NoiseCharactersTest.java
However, using the loop in test cases is not a good test practice.
We analyzed the relevant Stack Overflow posts and summarized four potential negatives it brings:
Loops make the test case more complex.
In most cases, a loop can be replaced with a data-driven test that is more readable.
Loops break the assert-for-one-thing thumb rule. I don't mean a single assert statement.
When a test fails, knowing the reason is more complicated.
Solution:
To avoid using the loop in the test, JUnit provides an annotation (i.e., @ParameteredTest), which can enable a test case to run multiple times with different parameters.
Therefore, the code can be refactored as:
Hi!
We notice that you use the loop structure in your test cases.
![image](https://user-images.githubusercontent.com/110402847/185106565-3540a76b-39c6-4720-84d4-4dc9a2915637.png)
For example, testInvalidChar() in NoiseCharactersTest.java
However, using the loop in test cases is not a good test practice.
We analyzed the relevant Stack Overflow posts and summarized four potential negatives it brings:
Solution:
To avoid using the loop in the test, JUnit provides an annotation (i.e., @ParameteredTest), which can enable a test case to run multiple times with different parameters.
Therefore, the code can be refactored as:
@test
@ParameteredTest
@@valuesource(invalidPatterns = {"", "1", "ABC", "\u21", "#x0001-#x0000"
, "#x0001 - #x - #x0000", "#x0000 #x0022"})
public void testInvalidChar(String i) {
assertThrows(IllegalArgumentException.class, () -> {
myFilter.add(i);
});
}
The text was updated successfully, but these errors were encountered: