Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] can anyone help me to take a look my exceptionhandler ?

Like this?

##### some JUnit 4 tests #####

package de.scrum_master.aop.demo;

import static org.junit.Assert.*;
import org.junit.Test;

public class MyTestCase {
    @Test public void testOne() {
        assertEquals("scrum-master.de", "scrum-" + "master" + ".de");
    }

    @Test public void testTwo() {
        throw new RuntimeException("Unexpected test exception");
    }

    @Test public void testThree() {
        fail("Not yet implemented");
    }

    @Test public void testFour() {
        throw new RuntimeException("Another unexpected test exception");
    }
}

##### an aspect taking care of exception logging #####

package de.scrum_master.aop.demo;

import org.junit.Test;

public aspect TestExceptionHandler {
    protected void captureThrowable(Throwable t) {
        System.err.println(t);
    }

    after() throwing (Throwable t) : execution(@Test * *(..)) {
        captureThrowable(t);
    }
}

I am not catching the exceptions, just logging them, so as to make your
tests fail as expected for unexpected exceptions. The output looks like
this:

> java.lang.RuntimeException: Unexpected test exception
> java.lang.AssertionError: Not yet implemented
> java.lang.RuntimeException: Another unexpected test exception



xianglong, 31.08.2012 08:49:
> @Alexander option c is my concern
> 
>> Yes, I think I can help you if you tell me (in prose, not in code)
>> what you want to achieve: Do you want to
>>
>> a) intercept and log the exceptions in your production code, then 
>> pass them through (i.e. let them happen)?
>>
>> b) intercept and log the exceptions in your production code, but 
>> *not* pass them through (i.e. catch them)?
>>
>> c) just avoid code duplication with try/catch blocks in your test 
>> cases?
>>
>> d) anything else?


Back to the top