itsallcode / junit5-system-extensions Goto Github PK
View Code? Open in Web Editor NEWJUnit5 extensions to test Java System.x functions
License: Eclipse Public License 2.0
JUnit5 extensions to test Java System.x functions
License: Eclipse Public License 2.0
Currently the implementation of the SystemExitGuard
extension replaces the existing security guard with a specialized one that intercepts the System.exit
. This one has now special permissions and it also has no relationship to a pre-existing one. Other than saving it and restoring it later that is.
What would be less invasive is if the replacement took the existing one as delegate and delegates all checks but the exit check to the original one.
ExitGuardSecurityManager
delegates all checks to the preexisting one (with exception of the intercepted system exit.SecurityManager which is used by ExitGuard
is deprecated in Java 17. Starting with Java 18 it requires argument -Djava.security.manager=allow
when starting the JVM, see
If the option is missing, this exception will be thrown:
UnsupportedOperation The Security Manager is deprecated and will be removed in a future release
We need to document this and provide a workaround similar to this one.
When the Capturable
is closed, getCapturedData()
fails with a NullPointerException
. Reason is that capturing is missing right before the internal stream is closed.
SystemOutGuard
@SysOut
capture()
on the Capturable
Capturable
getCapturedData()
NullPointerException
getCapturedData()
returns data captured right before the stream was closed.
One of the problems we encountered when migrating from system-rules to your library is that there is no way to mute console output. See https://stefanbirkner.github.io/system-rules/index.html#SystemErrAndOutRule for the documentation. As the result, the output of the tests gets into the log.
Is it possible to add similar functionality to your library?
Allow asserting System.exit(int)
In test classes annotated with @ExtendWith(SystemErrGuard.class)
and with multiple test cases that sniff the System.Err
, the logs are truncated after the execution of the first test case.
An instance of java.util.logging.Logger
uses by default ConsoleHandler
that is bounded to a reference of the System.Err
.
This binding happens upon creation of the Consolehandler
, which is lazily instantiated, for example when logging a message with the logger for the first time (ie, LOGGER.info("message")
).
If the System.Err
is changed (ie, System.setErr(newSystemStream)
), all the already existent instances of ConsoleHandler
are not updated, so they don't log to the new System.Err
.
import java.util.logging.Logger;
public final class TesteableClass {
public static final Logger LOGGER = Logger.getLogger(TesteableClass.class.getName());
public static String testeableMethod() {
LOGGER.info("TESTEABLE LOG MESSAGE");
}
}
@ExtendWith(SystemErrGuard.class)
class TestClass {
@Test
void firstTestCase(final Capturable stream) {
stream.capture();
TesteableClass.testeableMethod();
assertThat(stream.getCapturedData(), containsString("TESTEABLE LOG MESSAGE"));
}
@Test
void secondTestCase(final Capturable stream) {
stream.capture();
TesteableClass.testeableMethod();
assertThat(stream.getCapturedData(), containsString("TESTEABLE LOG MESSAGE"));
}
}
ConsoleHandler
before executing each tests and disabling the use of the parent ConsoleHandler
(cause if not it would log twice, once for each handler, during the execution of the first test), for example: @BeforeEach
void beforeEach() {
TesteableClass.LOGGER.setUseParentHandlers(false);
final Handler[] handlers = TesteableClass.LOGGER.getHandlers();
for (final Handler handler : handlers) {
TesteableClass.LOGGER.removeHandler(handler);
}
TesteableClass.LOGGER.addHandler(new ConsoleHandler());
}
Tests should pass and "TESTEABLE LOG MESSAGE" should be visible twice in the console.
The project collected a number of code smells that need to be addressed in the version up to 1.2.0
As a developer
I want to get a report from a Continuous Integration Build
in order to only complete pull requests where all tests are green.
See https://github.com/itsallcode/junit5-system-extensions/actions/runs/9853004485/job/27202629886
[INFO] * Upload of locally staged artifacts finished.
[INFO] * Closing staging repository with ID "orgitsallcode-1111".
Waiting for operation to complete...
..............................
Error: Rule failure while trying to close staging repository with ID "orgitsallcode-1111".
Error:
Error: Nexus Staging Rules Failure Report
Error: ==================================
Error:
Error: Repository "orgitsallcode-1111" failures
Error: Rule "signature-staging" failures
Error: * Missing Signature: '/org/itsallcode/junit5-system-extensions/1.2.1/junit5-system-extensions-1.2.1.pom.asc' does not exist for 'junit5-system-extensions-1.2.1.pom'.
Error: * Missing Signature: '/org/itsallcode/junit5-system-extensions/1.2.1/junit5-system-extensions-1.2.1-sources.jar.asc' does not exist for 'junit5-system-extensions-1.2.1-sources.jar'.
Error: * Missing Signature: '/org/itsallcode/junit5-system-extensions/1.2.1/junit5-system-extensions-1.2.1-javadoc.jar.asc' does not exist for 'junit5-system-extensions-1.2.1-javadoc.jar'.
Error: * Missing Signature: '/org/itsallcode/junit5-system-extensions/1.2.1/junit5-system-extensions-1.2.1.jar.asc' does not exist for 'junit5-system-extensions-1.2.1.jar'.
Error:
Error:
Error: Cleaning up local stage directory after a Rule failure during close of staging repositories: [orgitsallcode-1111]
Error: * Deleting context 546ea6ce74787e.properties
Error: Cleaning up remote stage repositories after a Rule failure during close of staging repositories: [orgitsallcode-1111]
Error: * Dropping failed staging repository with ID "orgitsallcode-1111" (Rule failure during close of staging repositories: [orgitsallcode-1111]).
Waiting for operation to complete...
....
Error: Remote staging finished with a failure: Staging rules failure!
JUnit 5 uses EPL as license (https://www.eclipse.org/legal/epl-2.0/). This module should use the same in order to make adoption easier for JUnit users.
As a developer
I want to test output sent to System.out.x()
in order to make sure that the output is correct.
We have problems using the plugin in a Java8 project:
[ERROR] /home/travis/build/checkstyle/checkstyle/src/test/java/com/puppycrawl/tools/checkstyle/MainTest.java:[27,49] cannot access org.itsallcode.junit.sysextensions.AssertExit
bad class file: /home/travis/.m2/repository/org/itsallcode/junit5-system-extensions/1.2.0/junit5-system-extensions-1.2.0.jar(org/itsallcode/junit/sysextensions/AssertExit.class)
class file has wrong version 55.0, should be 52.0
Please indicate your position on the minimum version of the JDK.
Building the project with Java 17 fails due to missing Javadoc comments
JAVA_HOME=$JAVA17_HOME mvn javadoc:aggregate
build-next-java.yml
workflow is activated againA declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.