001 /* 002 * Created on Mar 19, 2007 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 005 * the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 011 * specific language governing permissions and limitations under the License. 012 * 013 * Copyright @2007-2011 the original author or authors. 014 */ 015 package org.fest.assertions.api; 016 017 import org.fest.assertions.internal.Failures; 018 019 /** 020 * Common failures. 021 * 022 * @author Alex Ruiz 023 * @author Yvonne Wang 024 * @author Joel Costigliola 025 */ 026 public final class Fail { 027 028 /** 029 * Sets wether we remove elements related to Fest from assertion error stack trace. 030 * @param removeFestRelatedElementsFromStackTrace flag. 031 */ 032 public static void setRemoveFestRelatedElementsFromStackTrace(boolean removeFestRelatedElementsFromStackTrace) { 033 Failures.instance().setRemoveFestRelatedElementsFromStackTrace(removeFestRelatedElementsFromStackTrace); 034 } 035 036 /** 037 * Fails with the given message. 038 * @param failureMessage error message. 039 * @throws AssertionError with the given message. 040 */ 041 public static void fail(String failureMessage) { 042 throw Failures.instance().failure(failureMessage); 043 } 044 045 /** 046 * Throws an {@link AssertionError} with the given message and with the {@link Throwable} that caused the failure. 047 * @param failureMessage the description of the failed assertion. It can be {@code null}. 048 * @param realCause cause of the error. 049 * @throws AssertionError with the given message and with the {@link Throwable} that caused the failure. 050 */ 051 public static void fail(String failureMessage, Throwable realCause) { 052 AssertionError error = Failures.instance().failure(failureMessage); 053 error.initCause(realCause); 054 throw error; 055 } 056 057 /** 058 * Throws an {@link AssertionError} with a message explaining that an expection of given exceptionClass type was expected to be thrown but had not been. 059 * @param exceptionClass the class exception that was expected to be thrown. 060 * @throws AssertionError with a message explaining that an expection of given exceptionClass type was expected to be thrown but had not been. 061 */ 062 public static void failBecauseExceptionWasNotThrown(Class<? extends Exception> exceptionClass) { 063 String message = String.format("Expected %s to be thrown", exceptionClass.getSimpleName()); 064 throw Failures.instance().failure(message); 065 } 066 067 /** 068 * This constructor is protected to make it possible to subclass this class. Since all its methods are static, there 069 * is no point on creating a new instance of it. 070 */ 071 protected Fail() {} 072 }