All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.softhouse.common.testlib.Thrower Maven / Gradle / Ivy

/*
 * Copyright 2013 Jonatan Jönsson
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package se.softhouse.common.testlib;

import javax.annotation.concurrent.Immutable;

import com.google.common.annotations.VisibleForTesting;

/**
 * 
 * Makes it possible to throw checked exceptions from methods that don't declare them. See
 * throwing-undeclared-checked for a more thorough discussion.
 * Used in tests to reach places where 3rd party code forces you to catch exceptions that
 * can't happen.
 * 
*/ @Immutable public final class Thrower { /** * Re-throws {@code checkedException} without declaring that it throws it * * @return doesn't actually return the exception to throw but removes the need to have * {@code return null;} if the calling code have a return type */ public static RuntimeException asUnchecked(final Exception checkedException) { return new Thrower().sneakyThrow(checkedException); } private Thrower() { } @SuppressWarnings("unchecked") // Takes advantage of the fact that the JVM allows any exceptions to be thrown @VisibleForTesting E sneakyThrow(Exception exception) throws E { throw (E) exception; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy