Enhancing JUnit myself, how to?

View: New views
14 Messages — Rating Filter:   Alert me  

Enhancing JUnit myself, how to?

by remoliechti :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi guys,

JUnit4 comes with a nice annotation that supports checking for a special exception:
@Test(expected=MyException.class)

Well, the exception contains an error code which should be checked as well, so I thought, why not enhancing JUnit to check this as well:
@Test(expected=MyException.class,errorCode=5)

So I downloaded the JUnit sources and customized the org.junit.Test annotation, as well as org.junit.internal.runners.MethodRoodie and org.junit.internal.runners.TestMethod to fit my needs.

Within eclipse, my new annotation is recognized by the code completion tool, but when running the tests it still uses the shipped junit version.
How can I exchange the junit jar file used by eclipse? Just replacing the jar within the junit plugins folder will crash eclipse since it recognizes the change...

Any ideas?
Maybe this enhancement could be fed back to the junit sources?

Thanks,
Remo


Re: Enhancing JUnit myself, how to?

by Alistair Israel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Oct 21, 2009 at 11:00 PM, remoliechti <remo.liechti@...> wrote:

> How can I exchange the junit jar file used by eclipse? Just replacing the jar within the junit plugins folder will crash eclipse since it recognizes the change...

Hi, Remo.

Have you tried changing the Eclipse project classpath? Right-click the
project, select "Properties" then "Java Build Path", then the
"Libraries" tab. You should see the "JUnit" library there. Just remove
it and then add your custom-built JAR in its place.

- alistair
--
http://alistairisrael.wordpress.com

Re: Enhancing JUnit myself, how to?

by Bogdan Mocanu-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Regarding the feeding of this change back to the JUnit sources, I think the most appropriate person to answer is (one of) the author(s) of the framework.

However, you need to think that changes like these can be found in a million combinations. I might, for example, want to have the errorCode not as a an int, but as a String, or as an enumeration item. I might also have multiple error codes, a major one and a minor one, and so on.

Therefore it is very unlikely that this change will get to the sources.

There is a mail sent by David Saff on 20th of Oct, regarding the feature prioritization on JUnit. You can read there what is the process of placing your request on the list of ToDos for JUnit.

Regards,
Bogdan

--- In junit@..., "remoliechti" <remo.liechti@...> wrote:

>
> Hi guys,
>
> JUnit4 comes with a nice annotation that supports checking for a special exception:
> @Test(expected=MyException.class)
>
> Well, the exception contains an error code which should be checked as well, so I thought, why not enhancing JUnit to check this as well:
> @Test(expected=MyException.class,errorCode=5)
>
> So I downloaded the JUnit sources and customized the org.junit.Test annotation, as well as org.junit.internal.runners.MethodRoodie and org.junit.internal.runners.TestMethod to fit my needs.
>
> Within eclipse, my new annotation is recognized by the code completion tool, but when running the tests it still uses the shipped junit version.
> How can I exchange the junit jar file used by eclipse? Just replacing the jar within the junit plugins folder will crash eclipse since it recognizes the change...
>
> Any ideas?
> Maybe this enhancement could be fed back to the junit sources?
>
> Thanks,
> Remo
>



Re: Enhancing JUnit myself, how to?

by remoliechti :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Alistair,

that's what I did. My jar is within the classpath, so eclipse recognizes this jar file on compilation time (supporting my customized annotation).

Never the less it does not use my jar file on running the unit tests :(

Regards,
Remo

--- In junit@..., Alistair Israel <aisrael@...> wrote:

>
> On Wed, Oct 21, 2009 at 11:00 PM, remoliechti <remo.liechti@...> wrote:
>
> > How can I exchange the junit jar file used by eclipse? Just replacing the jar within the junit plugins folder will crash eclipse since it recognizes the change...
>
> Hi, Remo.
>
> Have you tried changing the Eclipse project classpath? Right-click the
> project, select "Properties" then "Java Build Path", then the
> "Libraries" tab. You should see the "JUnit" library there. Just remove
> it and then add your custom-built JAR in its place.
>
> - alistair
> --
> http://alistairisrael.wordpress.com
>



Re: Enhancing JUnit myself, how to?

by remoliechti :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That's true, but it would be possible to enhance the annotation in a way where this would still work:

@Test(expected=MyException.class,errorCodeValue=myString,errorCodeMethod=getErrorCode,errorCodeType=String.class)

I think that'be sufficient for most cases. default values for method name and type could be set as well(most likely int).


--- In junit@..., "Bogdan" <bogdan.mocanu.notifications@...> wrote:

>
> Regarding the feeding of this change back to the JUnit sources, I think the most appropriate person to answer is (one of) the author(s) of the framework.
>
> However, you need to think that changes like these can be found in a million combinations. I might, for example, want to have the errorCode not as a an int, but as a String, or as an enumeration item. I might also have multiple error codes, a major one and a minor one, and so on.
>
> Therefore it is very unlikely that this change will get to the sources.
>
> There is a mail sent by David Saff on 20th of Oct, regarding the feature prioritization on JUnit. You can read there what is the process of placing your request on the list of ToDos for JUnit.
>
> Regards,
> Bogdan
>
> --- In junit@..., "remoliechti" <remo.liechti@> wrote:
> >
> > Hi guys,
> >
> > JUnit4 comes with a nice annotation that supports checking for a special exception:
> > @Test(expected=MyException.class)
> >
> > Well, the exception contains an error code which should be checked as well, so I thought, why not enhancing JUnit to check this as well:
> > @Test(expected=MyException.class,errorCode=5)
> >
> > So I downloaded the JUnit sources and customized the org.junit.Test annotation, as well as org.junit.internal.runners.MethodRoodie and org.junit.internal.runners.TestMethod to fit my needs.
> >
> > Within eclipse, my new annotation is recognized by the code completion tool, but when running the tests it still uses the shipped junit version.
> > How can I exchange the junit jar file used by eclipse? Just replacing the jar within the junit plugins folder will crash eclipse since it recognizes the change...
> >
> > Any ideas?
> > Maybe this enhancement could be fed back to the junit sources?
> >
> > Thanks,
> > Remo
> >
>



Re: Re: Enhancing JUnit myself, how to?

by Malte Finsterwalder-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Which exceptions have an error code?
I never had to check errorCodes in exceptions.
Am I right, that only your custom build exceptions have an error code?
Than I think this extension should not be integrated into JUnit, since
it's not applicable to many other situations.
Or we need an extension mechanism in JUnit that makes creating new
Annotation possible.

Greetings,

2009/10/22 remoliechti <remo.liechti@...>:

> That's true, but it would be possible to enhance the annotation in a way where this would still work:
>
> @Test(expected=MyException.class,errorCodeValue=myString,errorCodeMethod=getErrorCode,errorCodeType=String.class)
>
> I think that'be sufficient for most cases. default values for method name and type could be set as well(most likely int).
>
>
> --- In junit@..., "Bogdan" <bogdan.mocanu.notifications@...> wrote:
>>
>> Regarding the feeding of this change back to the JUnit sources, I think the most appropriate person to answer is (one of) the author(s) of the framework.
>>
>> However, you need to think that changes like these can be found in a million combinations. I might, for example, want to have the errorCode not as a an int, but as a String, or as an enumeration item. I might also have multiple error codes, a major one and a minor one, and so on.
>>
>> Therefore it is very unlikely that this change will get to the sources.
>>
>> There is a mail sent by David Saff on 20th of Oct, regarding the feature prioritization on JUnit. You can read there what is the process of placing your request on the list of ToDos for JUnit.
>>
>> Regards,
>> Bogdan
>>
>> --- In junit@..., "remoliechti" <remo.liechti@> wrote:
>> >
>> > Hi guys,
>> >
>> > JUnit4 comes with a nice annotation that supports checking for a special exception:
>> > @Test(expected=MyException.class)
>> >
>> > Well, the exception contains an error code which should be checked as well, so I thought, why not enhancing JUnit to check this as well:
>> > @Test(expected=MyException.class,errorCode=5)
>> >
>> > So I downloaded the JUnit sources and customized the org.junit.Test annotation, as well as org.junit.internal.runners.MethodRoodie and org.junit.internal.runners.TestMethod to fit my needs.
>> >
>> > Within eclipse, my new annotation is recognized by the code completion tool, but when running the tests it still uses the shipped junit version.
>> > How can I exchange the junit jar file used by eclipse? Just replacing the jar within the junit plugins folder will crash eclipse since it recognizes the change...
>> >
>> > Any ideas?
>> > Maybe this enhancement could be fed back to the junit sources?
>> >
>> > Thanks,
>> > Remo
>> >
>>
>
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>
>

Re: Re: Enhancing JUnit myself, how to?

by Alistair Israel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Oct 22, 2009 at 2:21 PM, remoliechti <remo.liechti@...> wrote:

> that's what I did. My jar is within the classpath, so eclipse recognizes this jar file on compilation time (supporting my customized annotation).
>
> Never the less it does not use my jar file on running the unit tests :(

Strange. I've built multiple 'custom builds' of JUnit, and not only
can I successfully compile and run my tests against them under
Eclipse, but they also work using Maven.

Maybe there's another JUnit JAR on the run-time classpath? Have you
checked your run configurations?

- alistair
--
http://alistairisrael.wordpress.com

Re: Enhancing JUnit myself, how to?

by Bogdan Mocanu-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have a tip here: run your test inside Eclipse, then after the tests are finished, open the Debug perspective and in the Debug view (or open directly that view) right click on the terminated node (I think the second one, in that tree), and click Properties. There you will see the complete command that Eclipse invoked, including all the jars that it placed in the classpath. There you can see what is it using, and why it isn't taking your version of JUnit.

Bogdan

--- In junit@..., Alistair Israel <aisrael@...> wrote:

>
> On Thu, Oct 22, 2009 at 2:21 PM, remoliechti <remo.liechti@...> wrote:
>
> > that's what I did. My jar is within the classpath, so eclipse recognizes this jar file on compilation time (supporting my customized annotation).
> >
> > Never the less it does not use my jar file on running the unit tests :(
>
> Strange. I've built multiple 'custom builds' of JUnit, and not only
> can I successfully compile and run my tests against them under
> Eclipse, but they also work using Maven.
>
> Maybe there's another JUnit JAR on the run-time classpath? Have you
> checked your run configurations?
>
> - alistair
> --
> http://alistairisrael.wordpress.com
>



Re: Enhancing JUnit myself, how to?

by remoliechti :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message




there is none. but anyhow, I found THE way how to do it:
implement your own annotation which you add on method level. then also implement an own junit test runner, that does a try catch around the test method invocation and checks the exception according the attributes set of the own annotation.


@RunWith(MyRunner.class)
public class MyTestClass {

@Test(expected=MyException.class)
@MyAnnotation(error = 16)
public void thetestMethod() {




public class MyRunner extends BlockJUnit4ClassRunner {

@Override
protected Statement methodInvoker(FrameworkMethod method, Object test) {
return new MyInvokeMethod(method, test);
}


public class MyInvokeMethod extends InvokeMethod{

private FrameworkMethod testMethod;

public SwisslogInvokeMethod(FrameworkMethod testMethod, Object target) {
super(testMethod, target);
this.testMethod= testMethod;
}

@Override
public void evaluate() throws Throwable {
try{
  super.evaluate();
}catch (Throwable e) {
MyException ex = getMyExceptionOutOfTheExceptionTree(e);
if(ex==null){
// throw original exception if MyException is not within the tree
throw e;
}

MyAnnotation annotation = testMethod.getAnnotation(MyAnnotation .class);

if(annotation.error()!=ex.getErrorCode()){
throw new Exception("Error code does not match. Expected <"+annotation.error()+"> but was actual <"+ex.getErrorCode()+">",e);
}

throw ex;
}
}


This just works PERFECT!






--- In junit@..., Alistair Israel <aisrael@...> wrote:

>
> On Thu, Oct 22, 2009 at 2:21 PM, remoliechti <remo.liechti@...> wrote:
>
> > that's what I did. My jar is within the classpath, so eclipse recognizes this jar file on compilation time (supporting my customized annotation).
> >
> > Never the less it does not use my jar file on running the unit tests :(
>
> Strange. I've built multiple 'custom builds' of JUnit, and not only
> can I successfully compile and run my tests against them under
> Eclipse, but they also work using Maven.
>
> Maybe there's another JUnit JAR on the run-time classpath? Have you
> checked your run configurations?
>
> - alistair
> --
> http://alistairisrael.wordpress.com
>



RE: Re: Enhancing JUnit myself, how to?

by kentb :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Remo,
 
This sounds like a good fit for a @Rule. See
org.junit.rules.ExpectedException for a model you can extend. The advantage
I can see for doing this as a rule instead of extending the annotation is
that someone can easily maintain the rule themself without having to change
the shared source of JUnit. Our philosophy with JUnit has always been to
provide the intersection of functionality needed by developer testers. The
kind of extensions Remo wants, while useful, don't seem to me to be broadly
enough useful to warrant adding complexity to the core of JUnit.
 
Regards,
 
Kent

  _____  

From: junit@... [mailto:junit@...] On Behalf Of
remoliechti
Sent: Wednesday, October 21, 2009 11:25 PM
To: junit@...
Subject: [junit] Re: Enhancing JUnit myself, how to?


 

That's true, but it would be possible to enhance the annotation in a way
where this would still work:

@Test(expected=MyException.class,errorCodeValue=myString,errorCodeMethod=get
ErrorCode,errorCodeType=String.class)

I think that'be sufficient for most cases. default values for method name
and type could be set as well(most likely int).

--- In junit@yahoogroups. <mailto:junit%40yahoogroups.com> com, "Bogdan"
<bogdan.mocanu.notifications@...> wrote:
>
> Regarding the feeding of this change back to the JUnit sources, I think
the most appropriate person to answer is (one of) the author(s) of the
framework.
>
> However, you need to think that changes like these can be found in a
million combinations. I might, for example, want to have the errorCode not
as a an int, but as a String, or as an enumeration item. I might also have
multiple error codes, a major one and a minor one, and so on.
>
> Therefore it is very unlikely that this change will get to the sources.
>
> There is a mail sent by David Saff on 20th of Oct, regarding the feature
prioritization on JUnit. You can read there what is the process of placing
your request on the list of ToDos for JUnit.
>
> Regards,
> Bogdan
>
> --- In junit@yahoogroups. <mailto:junit%40yahoogroups.com> com,
"remoliechti" <remo.liechti@> wrote:
> >
> > Hi guys,
> >
> > JUnit4 comes with a nice annotation that supports checking for a special
exception:
> > @Test(expected=MyException.class)
> >
> > Well, the exception contains an error code which should be checked as
well, so I thought, why not enhancing JUnit to check this as well:
> > @Test(expected=MyException.class,errorCode=5)
> >
> > So I downloaded the JUnit sources and customized the org.junit.Test
annotation, as well as org.junit.internal.runners.MethodRoodie and
org.junit.internal.runners.TestMethod to fit my needs.
> >
> > Within eclipse, my new annotation is recognized by the code completion
tool, but when running the tests it still uses the shipped junit version.
> > How can I exchange the junit jar file used by eclipse? Just replacing
the jar within the junit plugins folder will crash eclipse since it
recognizes the change...
> >
> > Any ideas?
> > Maybe this enhancement could be fed back to the junit sources?
> >
> > Thanks,
> > Remo
> >
>






[Non-text portions of this message have been removed]


Re: Enhancing JUnit myself, how to?

by Cédric Beust ♔ :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Oct 21, 2009 at 8:00 AM, remoliechti <remo.liechti@...> wrote:

> Well, the exception contains an error code which should be checked as well,
> so I thought, why not enhancing JUnit to check this as well:
> @Test(expected=MyException.class,errorCode=5)
>
I'm guessing "errorCode" is specific to your exception class?  If so, it's
unlikely that this would be useful to JUnit users in general.

On the other hand, something that I've found useful in TestNG is being able
to test the error message against a regular exception:

@Test(expectedException=MyException.class, errorMessage = ".*Too big.*")

You might want to consider this for JUnit since it's applicable regardless
of the exception class.

--
Cédric


[Non-text portions of this message have been removed]


Re: Enhancing JUnit myself, how to?

by David Saff :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/10/22 Cédric Beust ♔ <cbeust@...>:

> On Wed, Oct 21, 2009 at 8:00 AM, remoliechti <remo.liechti@...> wrote:
>
>> Well, the exception contains an error code which should be checked as well,
>> so I thought, why not enhancing JUnit to check this as well:
>> @Test(expected=MyException.class,errorCode=5)
>>
> I'm guessing "errorCode" is specific to your exception class?  If so, it's
> unlikely that this would be useful to JUnit users in general.
>
> On the other hand, something that I've found useful in TestNG is being able
> to test the error message against a regular exception:
>
> @Test(expectedException=MyException.class, errorMessage = ".*Too big.*")
>
> You might want to consider this for JUnit since it's applicable regardless
> of the exception class.

Already considered and implemented as a Rule in JUnit 4.7!

   David

>
> --
> Cédric
>
>
> [Non-text portions of this message have been removed]
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>
>

Re: Enhancing JUnit myself, how to?

by Cédric Beust ♔ :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Oct 22, 2009 at 7:33 PM, David Saff <david@...> wrote:

> > On the other hand, something that I've found useful in TestNG is being
> able
> > to test the error message against a regular exception:
> >
> > @Test(expectedException=MyException.class, errorMessage = ".*Too big.*")
> >
> > You might want to consider this for JUnit since it's applicable
> regardless
> > of the exception class.
>
> Already considered and implemented as a Rule in JUnit 4.7!


Nice!  But I thought that Rules were defined for an entire class, so is it
possible to have two different error messages for two methods that belong to
the same class?

--
Cédric


[Non-text portions of this message have been removed]


Re: Enhancing JUnit myself, how to?

by David Saff :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/10/22 Cédric Beust ♔ <cbeust@...>:

> On Thu, Oct 22, 2009 at 7:33 PM, David Saff <david@...> wrote:
>
>> > On the other hand, something that I've found useful in TestNG is being
>> able
>> > to test the error message against a regular exception:
>> >
>> > @Test(expectedException=MyException.class, errorMessage = ".*Too big.*")
>> >
>> > You might want to consider this for JUnit since it's applicable
>> regardless
>> > of the exception class.
>>
>> Already considered and implemented as a Rule in JUnit 4.7!
>
>
> Nice!  But I thought that Rules were defined for an entire class, so is it
> possible to have two different error messages for two methods that belong to
> the same class?

Yes!  See http://github.com/KentBeck/junit/blob/master/src/main/java/org/junit/rules/ExpectedException.java
(until I get the online javadoc fixed...)

   David Saff