org.itsallcode.junit.sysextensions.SystemErrGuard Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of junit5-system-extensions Show documentation
Show all versions of junit5-system-extensions Show documentation
These extensions help testing behavior that is related to `System.x` calls like `exit(int).
package org.itsallcode.junit.sysextensions;
/*-
* #%L
* JUnit5 System Extensions
* %%
* Copyright (C) 2018 itsallcode.org
* %%
* All rights reserved. This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v2.0 which accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v20.html
* #L%
*/
import java.io.PrintStream;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.itsallcode.io.Capturable;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
public class SystemErrGuard extends AbstractSystemOutputGuard
{
private static final Namespace NAMESPACE = Namespace.create(SystemErrGuard.class);
/**
* This annotation can be used on a parameter of type {@link Capturable} to
* ensure that the placeholder for System.err
is injected.
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysErr
{
}
@Override
protected Namespace getNamespace()
{
return NAMESPACE;
}
@Override
protected PrintStream getSystemStream()
{
return System.err;
}
@Override
protected void setSystemStream(final PrintStream systemStream)
{
System.setErr(systemStream);
}
@Override
protected Class getParameterAnnotation()
{
return SysErr.class;
}
}