Search in sources :

Example 1 with Protectable

use of junit.framework.Protectable in project jmeter by apache.

the class JUnitSampler method initializeTestObject.

/**
     * Initialize test object
     */
private void initializeTestObject() {
    String rlabel = getConstructorString();
    if (rlabel.length() == 0) {
        rlabel = JUnitSampler.class.getName();
    }
    this.testObject = getClassInstance(className, rlabel);
    if (this.testObject != null) {
        initMethodObjects(this.testObject);
        final Method m = getMethod(this.testObject, methodName);
        if (getJunit4()) {
            Class<? extends Throwable> expectedException = None.class;
            long timeout = 0;
            Test annotation = m.getAnnotation(Test.class);
            if (null != annotation) {
                expectedException = annotation.expected();
                timeout = annotation.timeout();
            }
            final AnnotatedTestCase at = new AnnotatedTestCase(m, expectedException, timeout);
            testCase = at;
            protectable = new Protectable() {

                @Override
                public void protect() throws Throwable {
                    at.runTest();
                }
            };
        } else {
            this.testCase = (TestCase) this.testObject;
            // Must be final to create instance
            final Object theClazz = this.testObject;
            protectable = new Protectable() {

                @Override
                public void protect() throws Throwable {
                    try {
                        m.invoke(theClazz, new Object[0]);
                    } catch (InvocationTargetException e) {
                        /*
                             * Calling a method via reflection results in wrapping any
                             * Exceptions in ITE; unwrap these here so runProtected can
                             * allocate them correctly.
                             */
                        Throwable t = e.getCause();
                        if (t != null) {
                            throw t;
                        }
                        throw e;
                    }
                }
            };
        }
        if (this.testCase != null) {
            this.testCase.setName(methodName);
        }
    }
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test) None(org.junit.Test.None) Protectable(junit.framework.Protectable)

Example 2 with Protectable

use of junit.framework.Protectable in project tomee by apache.

the class NumberedTestCase method run.

protected void run(final TestResult result, final Method testMethod) {
    final Test test = createTest(testMethod);
    result.startTest(test);
    final Protectable p = new Protectable() {

        public void protect() throws Throwable {
            runTestMethod(testMethod);
        }
    };
    // System.out.println(">>" + NumberedTestCase.class.getName() + "> started: " + testMethod.toGenericString());
    result.runProtected(test, p);
    result.endTest(test);
// System.out.println(">>" + NumberedTestCase.class.getName() + "> done: " + testMethod.toGenericString());
}
Also used : Test(junit.framework.Test) Protectable(junit.framework.Protectable)

Example 3 with Protectable

use of junit.framework.Protectable in project junit4 by junit-team.

the class TestSetup method run.

@Override
public void run(final TestResult result) {
    Protectable p = new Protectable() {

        public void protect() throws Exception {
            setUp();
            basicRun(result);
            tearDown();
        }
    };
    result.runProtected(this, p);
}
Also used : Protectable(junit.framework.Protectable)

Aggregations

Protectable (junit.framework.Protectable)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Test (junit.framework.Test)1 Test (org.junit.Test)1 None (org.junit.Test.None)1