Pre-check
- [x] I am sure that all the content I provide is in English.
Search before asking
- [x] I had searched in the issues and found no similar feature requirement.
Apache Dubbo Component
Java SDK (apache/dubbo)
Descriptions
Part I: Remove System.out
The Key Problems of Unit Tests that Use System.* Calls (like System.out.println
)
1. Violates unit testing best practices (test isolation principle).
2. Causes thread blocking due to synchronized I/O operations.
3. Slows down parallel test execution.
4. Makes test output harder to track.
Recommended Refactoring Approaches 1. Remove these codes from unit tests, at least most of them. 2. Replace System.out.println with proper slf4j/Log4j2 (perfered) if the test info is very very important. 3. For validation needs, use assertion libraries.
Examples
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class MyTest {
private static final Logger logger = LoggerFactory.getLogger(MyTest.class);
@Test
void testMethod() {
// remove System.out.println("test info") or log the test info if needed.
logger.debug("Debug info: a={}, b={}", a, b);
// Actual test assertions...
}
}
Additional Recommendations If Log the Test Info
1. Configure log levels appropriately (DEBUG/INFO) if log the test info.
2. Use parameterized logging to avoid string concatenation. (like logger.info("my info={}, {}", a, b);
)
Part I: Remove JUnit4
Upgrading from JUnit 4 to JUnit 5 is highly recommended and often necessary for modern Java development, especially with JDK 17+: 1. End of Active Development: JUnit 4 is effectively in maintenance mode. While critical bugs might still be fixed, it receives no significant new features or improvements. JUnit 5 is the actively developed, modern standard. 2. Performance & Scalability: JUnit 5's architecture, particularly its extension model (Extension API vs JUnit 4's Runner model) and built-in support for parallel test execution, generally offers better performance, especially for large test suites. 3. JDK 17+ Compatibility: Module System (JPMS): JUnit 5 is fully compatible with Java's Module System (JPMS) introduced in JDK 9+, ensuring smooth operation on JDK 17+. JUnit 4 relies on workarounds or specific configurations which become increasingly fragile or break. 4. Plugins: tools like the maven-failsafe-plugin (and maven-surefire-plugin) have evolved to prioritize JUnit 5 support. Running JUnit4 tests under these plugins on JDK 17+ often requires specific, non-trivial configuration or dependencies (like the junit-vintage-engine) and can lead to compatibility headaches or failures that simply don't occur with JUnit 5. 5. Modern Features: JUnit 5 introduces significant improvements: - Parameterized Tests: Much more powerful and flexible (@ParameterizedTest). - Dynamic Tests: Generate tests at runtime (@TestFactory). - Nested Tests: Better structure for complex test classes (@Nested). - Conditional Test Execution: Fine-grained control over when tests run (@EnabledOnJre, @DisabledIf, etc.). - Extension Model: Cleaner, more composable than @RunWith and @Rule. - Lambda Support: First-class support for assertions and assumptions using lambdas. - Display Names: Customizable, human-readable test and class names (@DisplayName). - Ecosystem Alignment: The broader Java testing ecosystem (mocking frameworks like Mockito, assertion libraries like AssertJ, build tools, IDEs) increasingly focuses its integration efforts and new features on JUnit 5.
Examples:
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@EnableDubbo
@ExtendWith(SpringExtension.class)
public class HelloTest {
@Test
public void hello() {
...
Assertions.assertTrue(result.contains("ok"));
}
}
MORE SIMILAR WANTED 1. integration test cases: https://github.com/apache/dubbo-integration-cases 2. sample test cases: https://github.com/apache/dubbo-samples
Related issues
No response
Are you willing to submit a pull request to fix on your own?
- [ ] Yes I am willing to submit a pull request on my own!
Code of Conduct
- [x] I agree to follow this project's Code of Conduct
Comment From: little-kid-an
Hello, can I be involved in this work
Comment From: RainYuY
Hello, can I be involved in this work
Sure. Just go ahead
Comment From: wcy666103
Hello, can I be involved in this work
May I ask if you are still doing it? I'm also interested in participating. @little-kid-an
Comment From: little-kid-an
Hello, can I be involved in this work
May I ask if you are still doing it? I'm also interested in participating. @little-kid-an
Yes, I have now completed the replacement or deletion of System.out in the common, rpc-api, and rpc-dubbo modules. If you also want to do it, we can divide the modules. @wcy666103
Comment From: little-kid-an
Hello, can I be involved in this work
May I ask if you are still doing it? I'm also interested in participating. @little-kid-an
I have completed this function.
Comment From: sunheyi6
@zrlw this issue has been resolved
Comment From: zrlw
@zrlw this issue has been resolved
Dubbo has many affiliated repositories which are still in progress:
1. integration test cases, https://github.com/apache/dubbo-integration-cases
e.g., the pom.xml of dubbo-test-runner
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>
- sample test cases: https://github.com/apache/dubbo-samples
e.g., the pom.xml of
dubbo-test-runner
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>