Search in sources :

Example 1 with ComparisonFailure

use of org.junit.ComparisonFailure in project junit4 by junit-team.

the class ComparisonFailureTest method compactFailureMessage.

@Test
public void compactFailureMessage() {
    ComparisonFailure failure = new ComparisonFailure("", expected, actual);
    assertEquals(message, failure.getMessage());
}
Also used : ComparisonFailure(org.junit.ComparisonFailure) Test(org.junit.Test)

Example 2 with ComparisonFailure

use of org.junit.ComparisonFailure in project intellij-community by JetBrains.

the class JUnitTreeByDescriptionHierarchyTest method testLongOutputPreservesTestName.

@Test
public void testLongOutputPreservesTestName() throws Exception {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < 1000; i++) {
        buf.append(DebugUtil.currentStackTrace());
    }
    final StringBuffer output = new StringBuffer();
    final JUnit4TestListener sender = createListener(output);
    final Description description = Description.createTestDescription("A", "a");
    sender.testFailure(new Failure(description, new ComparisonFailure(buf.toString(), buf.toString(), "diff" + buf.toString())));
    final String startMessage = "##teamcity[enteredTheMatrix]\n\n" + "##teamcity[testFailed name='A.a' ";
    assertEquals(startMessage, StringUtil.convertLineSeparators(output.toString()).substring(0, startMessage.length()));
}
Also used : Description(org.junit.runner.Description) ComparisonFailure(org.junit.ComparisonFailure) JUnit4TestListener(com.intellij.junit4.JUnit4TestListener) Failure(org.junit.runner.notification.Failure) ComparisonFailure(org.junit.ComparisonFailure) Test(org.junit.Test)

Example 3 with ComparisonFailure

use of org.junit.ComparisonFailure in project liquibase by liquibase.

the class DatabaseTestTemplate method test.

private void test(DatabaseTest test, Set<Database> databasesToTestOn) throws Exception {
    for (Database database : databasesToTestOn) {
        if (database instanceof SQLiteDatabase) {
            //todo: find how to get tests to run correctly on SQLite
            continue;
        }
        JdbcExecutor writeExecutor = new JdbcExecutor();
        writeExecutor.setDatabase(database);
        ExecutorService.getInstance().setExecutor(database, writeExecutor);
        LockService lockService = LockServiceFactory.getInstance().getLockService(database);
        lockService.reset();
        if (database.getConnection() != null) {
            lockService.forceReleaseLock();
        }
        try {
            test.performTest(database);
        } catch (ComparisonFailure e) {
            String newMessage = "Database Test Failure on " + database;
            if (e.getMessage() != null) {
                newMessage += ": " + e.getMessage();
            }
            ComparisonFailure newError = new ComparisonFailure(newMessage, e.getExpected(), e.getActual());
            newError.setStackTrace(e.getStackTrace());
            throw newError;
        } catch (AssertionError e) {
            e.printStackTrace();
            String newMessage = "Database Test Failure on " + database;
            if (e.getMessage() != null) {
                newMessage += ": " + e.getMessage();
            }
            AssertionError newError = new AssertionError(newMessage);
            newError.setStackTrace(e.getStackTrace());
            throw newError;
        } catch (MigrationFailedException e) {
            e.printStackTrace();
            String newMessage = "Database Test Failure on " + database;
            if (e.getMessage() != null) {
                newMessage += ": " + e.getMessage();
            }
            AssertionError newError = new AssertionError(newMessage);
            newError.setStackTrace(e.getStackTrace());
            throw newError;
        } catch (Exception e) {
            e.printStackTrace();
            String newMessage = "Database Test Exception on " + database;
            if (e.getMessage() != null) {
                newMessage += ": " + e.getMessage();
            }
            Exception newError = e.getClass().getConstructor(String.class).newInstance(newMessage);
            if (e.getCause() == null) {
                newError.setStackTrace(e.getStackTrace());
            } else {
                newError.setStackTrace(e.getCause().getStackTrace());
            }
            throw newError;
        } finally {
            if (database.getConnection() != null && !database.getAutoCommitMode()) {
                database.rollback();
            }
        }
    }
}
Also used : LockService(liquibase.lockservice.LockService) MigrationFailedException(liquibase.exception.MigrationFailedException) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) ComparisonFailure(org.junit.ComparisonFailure) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) Database(liquibase.database.Database) JdbcExecutor(liquibase.executor.jvm.JdbcExecutor) MigrationFailedException(liquibase.exception.MigrationFailedException)

Example 4 with ComparisonFailure

use of org.junit.ComparisonFailure in project spock by spockframework.

the class JUnitSupervisor method convertToComparisonFailure.

// enables IDE support (diff dialog)
private Throwable convertToComparisonFailure(Throwable exception) {
    assert isFailedEqualityComparison(exception);
    ConditionNotSatisfiedError conditionNotSatisfiedError = (ConditionNotSatisfiedError) exception;
    Condition condition = conditionNotSatisfiedError.getCondition();
    ExpressionInfo expr = condition.getExpression();
    String actual = renderValue(expr.getChildren().get(0).getValue());
    String expected = renderValue(expr.getChildren().get(1).getValue());
    ComparisonFailure failure = new SpockComparisonFailure(condition, expected, actual);
    failure.setStackTrace(exception.getStackTrace());
    if (conditionNotSatisfiedError.getCause() != null) {
        failure.initCause(conditionNotSatisfiedError.getCause());
    }
    return failure;
}
Also used : ComparisonFailure(org.junit.ComparisonFailure)

Example 5 with ComparisonFailure

use of org.junit.ComparisonFailure in project ceylon-compiler by ceylon.

the class CeylonTestRunner method executeTest.

private Failure executeTest(Method child, Description description) {
    Failure failure = null;
    try {
        Object instance;
        if (Modifier.isStatic(child.getModifiers())) {
            instance = null;
        } else {
            instance = testClass.newInstance();
        }
        Long previousCount = null;
        if (failureCountGetter != null)
            previousCount = (Long) failureCountGetter.invoke(null);
        child.invoke(instance);
        if (failureCountGetter != null) {
            Long newCount = (Long) failureCountGetter.invoke(null);
            if (!newCount.equals(previousCount))
                failure = new Failure(description, new AssertionError("check() failed: " + (newCount - previousCount) + " errors"));
        }
    } catch (InvocationTargetException e) {
        Throwable cause = e.getCause();
        StackTraceElement[] st = cause.getStackTrace();
        if ("ceylon.language.AssertionError".equals(cause.getClass().getName())) {
            AssertionError error = new AssertionError(cause.getMessage());
            error.setStackTrace(cause.getStackTrace());
            failure = new Failure(description, error);
        } else if ("com.redhat.ceylon.compiler.java.test.runtime.AssertionFailed".equals(cause.getClass().getName())) {
            AssertionError error = new AssertionError(cause.getMessage());
            error.setStackTrace(cause.getStackTrace());
            failure = new Failure(description, error);
        } else if ("com.redhat.ceylon.compiler.java.test.runtime.ComparisonFailed".equals(cause.getClass().getName())) {
            Object expected = get(cause, "getExpected");
            Object got = get(cause, "getGot");
            ComparisonFailure error = new ComparisonFailure(cause.getMessage(), String.valueOf(expected), String.valueOf(got));
            error.setStackTrace(cause.getStackTrace());
            failure = new Failure(description, error);
        } else {
            failure = new Failure(description, e);
        }
    } catch (Exception e) {
        failure = new Failure(description, e);
    } catch (AssertionError e) {
        failure = new Failure(description, e);
    }
    return failure;
}
Also used : ComparisonFailure(org.junit.ComparisonFailure) Failure(org.junit.runner.notification.Failure) ComparisonFailure(org.junit.ComparisonFailure) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

ComparisonFailure (org.junit.ComparisonFailure)5 Test (org.junit.Test)2 Failure (org.junit.runner.notification.Failure)2 JUnit4TestListener (com.intellij.junit4.JUnit4TestListener)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Database (liquibase.database.Database)1 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)1 MigrationFailedException (liquibase.exception.MigrationFailedException)1 JdbcExecutor (liquibase.executor.jvm.JdbcExecutor)1 LockService (liquibase.lockservice.LockService)1 Description (org.junit.runner.Description)1