Search in sources :

Example 96 with TestCase

use of junit.framework.TestCase in project android_frameworks_base by AOSPA.

the class TestCaseCollector method addSingleTestMethod.

protected void addSingleTestMethod(Class<?> clazz, String method) {
    if (!(mFilter.accept(clazz))) {
        throw new RuntimeException("Test class must be derived from UiAutomatorTestCase");
    }
    try {
        TestCase testCase = (TestCase) clazz.newInstance();
        testCase.setName(method);
        mTestCases.add(testCase);
    } catch (InstantiationException e) {
        mTestCases.add(error(clazz, "InstantiationException: could not instantiate " + "test class. Class: " + clazz.getName()));
    } catch (IllegalAccessException e) {
        mTestCases.add(error(clazz, "IllegalAccessException: could not instantiate " + "test class. Class: " + clazz.getName()));
    }
}
Also used : TestCase(junit.framework.TestCase)

Example 97 with TestCase

use of junit.framework.TestCase in project intellij-community by JetBrains.

the class TestRunnerUtil method createMethodSuite.

private static Test createMethodSuite(JUnit3IdeaTestRunner runner, Class testClass, String methodName) {
    runner.clearStatus();
    try {
        Constructor constructor = testClass.getConstructor(new Class[] { String.class });
        return (Test) constructor.newInstance(new Object[] { methodName });
    } catch (NoSuchMethodException e) {
        try {
            Constructor constructor = testClass.getConstructor(new Class[0]);
            TestCase test = (TestCase) constructor.newInstance(new Object[0]);
            test.setName(methodName);
            return test;
        } catch (ClassCastException e1) {
            boolean methodExists;
            try {
                //noinspection SSBasedInspection
                testClass.getMethod(methodName, new Class[0]);
                methodExists = true;
            } catch (NoSuchMethodException e2) {
                methodExists = false;
            }
            if (!methodExists) {
                String error = MessageFormat.format(ourBundle.getString("junit.method.not.found"), new Object[] { methodName });
                String message = MessageFormat.format(ourBundle.getString("junit.cannot.instantiate.tests"), new Object[] { error });
                return new FailedTestCase(testClass, methodName, message, null);
            }
            runner.runFailed(MessageFormat.format(ourBundle.getString("junit.class.not.derived"), new Object[] { testClass.getName() }));
            return null;
        } catch (Exception e1) {
            String message = MessageFormat.format(ourBundle.getString("junit.cannot.instantiate.tests"), new Object[] { e1.toString() });
            return new FailedTestCase(testClass, methodName, message, e1);
        }
    } catch (Throwable e) {
        String message = MessageFormat.format(ourBundle.getString("junit.cannot.instantiate.tests"), new Object[] { e.toString() });
        return new FailedTestCase(testClass, methodName, message, e);
    }
}
Also used : Test(junit.framework.Test) TestCase(junit.framework.TestCase) Constructor(java.lang.reflect.Constructor) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 98 with TestCase

use of junit.framework.TestCase in project kotlin by JetBrains.

the class JsUnitTestReporter method createTestSuiteAndFlush.

@NotNull
public TestSuite createTestSuiteAndFlush() {
    TestSuite suite = new TestSuite("!");
    Collection<String> newFinishedTests = getNewFinishedTests();
    for (String test : newFinishedTests) {
        //NOTE: well, it is a test
        //noinspection JUnitTestCaseWithNoTests
        suite.addTest(new TestCase(test) {

            @Override
            protected void runTest() throws Throwable {
                Boolean result = getResult(getName());
                if (!result) {
                    Collection<String> errorMessages = getErrors(getName());
                    StringBuilder sb = new StringBuilder();
                    for (String error : errorMessages) {
                        sb.append(error);
                    }
                    eraseTestInfo(getName());
                    Assert.fail(sb.toString());
                }
                eraseTestInfo(getName());
            }
        });
    }
    processedTests.addAll(newFinishedTests);
    return suite;
}
Also used : TestSuite(junit.framework.TestSuite) TestCase(junit.framework.TestCase) Collection(java.util.Collection) NotNull(org.jetbrains.annotations.NotNull)

Example 99 with TestCase

use of junit.framework.TestCase in project android_frameworks_base by DirtyUnicorns.

the class TestMethod method instantiateTest.

@SuppressWarnings("unchecked")
private TestCase instantiateTest(Class testCaseClass, String testName) throws InvocationTargetException, IllegalAccessException, InstantiationException {
    Constructor[] constructors = testCaseClass.getConstructors();
    if (constructors.length == 0) {
        return instantiateTest(testCaseClass.getSuperclass(), testName);
    } else {
        for (Constructor constructor : constructors) {
            Class[] params = constructor.getParameterTypes();
            if (noargsConstructor(params)) {
                TestCase test = ((Constructor<? extends TestCase>) constructor).newInstance();
                // JUnit will run just the one test if you call
                // {@link TestCase#setName(String)}
                test.setName(testName);
                return test;
            } else if (singleStringConstructor(params)) {
                return ((Constructor<? extends TestCase>) constructor).newInstance(testName);
            }
        }
    }
    throw new RuntimeException("Unable to locate a constructor for " + testCaseClass.getName());
}
Also used : TestCase(junit.framework.TestCase) Constructor(java.lang.reflect.Constructor)

Example 100 with TestCase

use of junit.framework.TestCase in project android_frameworks_base by DirtyUnicorns.

the class TestSuiteBuilder method build.

/**
     * Call this method once you've configured your builder as desired.
     *
     * @return The suite containing the requested tests.
     */
public final TestSuite build() {
    rootSuite = new TestSuite(getSuiteName());
    // Keep track of current class so we know when to create a new sub-suite.
    currentClassname = null;
    try {
        for (TestMethod test : testGrouping.getTests()) {
            if (satisfiesAllPredicates(test)) {
                addTest(test);
            }
        }
        if (testCases.size() > 0) {
            for (TestCase testCase : testCases) {
                if (satisfiesAllPredicates(new TestMethod(testCase))) {
                    addTest(testCase);
                }
            }
        }
    } catch (Exception exception) {
        Log.i("TestSuiteBuilder", "Failed to create test.", exception);
        TestSuite suite = new TestSuite(getSuiteName());
        suite.addTest(new FailedToCreateTests(exception));
        return suite;
    }
    return rootSuite;
}
Also used : TestSuite(junit.framework.TestSuite) TestCase(junit.framework.TestCase)

Aggregations

TestCase (junit.framework.TestCase)129 TestSuite (junit.framework.TestSuite)36 Test (junit.framework.Test)22 TestListener (junit.framework.TestListener)17 TestResult (junit.framework.TestResult)13 ArrayList (java.util.ArrayList)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)10 AssertionFailedError (junit.framework.AssertionFailedError)10 Constructor (java.lang.reflect.Constructor)8 Enumeration (java.util.Enumeration)6 Context (android.content.Context)5 PackageManager (android.content.pm.PackageManager)5 ResolveInfo (android.content.pm.ResolveInfo)5 Bundle (android.os.Bundle)5 HandlerThread (android.os.HandlerThread)5 ShellUiAutomatorBridge (com.android.uiautomator.core.ShellUiAutomatorBridge)5 Tracer (com.android.uiautomator.core.Tracer)5 UiAutomationShellWrapper (com.android.uiautomator.core.UiAutomationShellWrapper)5 Field (java.lang.reflect.Field)5 HashSet (java.util.HashSet)5