Bug 47803

Summary: Support for JUnit 4.x tests defined by annotations
Product: JMeter - Now in Github Reporter: Brian <Brian.Sweeney>
Component: MainAssignee: JMeter issues mailing list <issues>
Status: RESOLVED FIXED    
Severity: enhancement    
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   
Attachments: Support for junit annotated test cases
updated annotations patch
test case using JUnit 4.x annotations
annotated test cases
updated annotations patch
patch to support junit 4.x style annotated tests

Description Brian 2009-09-08 10:39:13 UTC
Created attachment 24229 [details]
Support for junit annotated test cases

The current version of jmeter doesn't recognize test cases specified by the
@Test annotation for junit 4.x+.  Support should be easy to support since junit
3 is already supported.  Basically, an annotated test case sampler should look
for the @Test annotation instead of classes that extend TestCase.

That's what I've done in the attached patch.  The annotated sampler is an
extension of the 3.x sampler.  I've overridden the sample method and a couple
of support methods to work with the new style tests.  That causes a difference
in behavior between instances of the 3.x and annotated samplers.  I initially
wanted to share the UI code and have it intelligently switch between 3.x and
annotated instances, but the methods available didn't easily permit that.  The
choice was between adding a very limited new UI type or hiding the actual junit
sampler instance behind a facade.  It seemed more in keeping with other
patterns to just make another UI class.  That seems like a reasonable choice
given that I expect users will know what they're trying to test (and which
version of junit they depend on) before they start writing test plans.

There are 2 remaining details where I could use some help.  First, I've added a
resource key (junit_4_request=JUnit Annotated Request) that needs to be
translated. I'm not sure what the process for that is.  Second, in the saved
test plan files, saves of my added class use the fully qualified name.  Other
classes seem to use short names.  Everything seems to work okay for me, but
there's probably a reason it looks different. I imagine that should be fixed.
Comment 1 Brian 2009-09-08 10:45:13 UTC
I forgot to mention that for this to all work, you'll need to be using a version of junit 4 instead of the junit 3.8.2 that normally comes with the current release of jmeter.
Comment 2 Sebb 2009-09-11 06:58:41 UTC
Thanks for the patch; I'll take a look at adding it soon.

Would it be possible to provide some example tests as well?
Comment 3 Brian 2009-09-11 10:07:05 UTC
Yes - working on that.  But in doing so I realized that some of the changes that I made busted things for new test plans (I think my saved test plans that I was using to make sure I didn't break things were working - leading me astray).  So I'm going to fix those issues today and re-submit.  I'll tack a test case on there.
Comment 4 Brian 2009-09-11 11:52:00 UTC
Created attachment 24250 [details]
updated annotations patch

My previous test included the prefix "test" on methods, which is why I didn't notice things were a bit broken.  This includes a fix that allows arbitrarily named methods.

@Suite, @BeforeClass and @AfterClass aren't currently supported.  It doesn't really seem to fit in with the framework of 1 test per sampler.
Comment 5 Brian 2009-09-11 11:54:39 UTC
Created attachment 24251 [details]
test case using JUnit 4.x annotations

Annotated test to exercise the new code.  If you don't run setup/teardown on the add() test, it should fail.  If you do run setup/teardown, it should work.
Comment 6 Sebb 2009-09-12 04:31:10 UTC
Thanks very much!

Two minor problems:
- there's no AL header in the test case file
- we don't use @author tags.

Would you mind fixing those please?

Also, if you get a chance, perhaps you could add some more Test annotations, e.g. expectedException and timeout, since these are handled by the code?
Comment 7 Brian 2009-09-14 07:03:27 UTC
Created attachment 24257 [details]
annotated test cases

Should flex the abilities of the patch that adds support for junit 4.x style tests
Comment 8 Brian 2009-09-14 07:04:35 UTC
Created attachment 24259 [details]
updated annotations patch

Fixes a timeout bug.
Comment 9 Brian 2009-09-14 07:08:54 UTC
Created attachment 24260 [details]
patch to support junit 4.x style annotated tests

Added the apache license to another file where I'd left it off.
Comment 10 Brian 2009-09-14 07:32:20 UTC
* Added AL headers to 2 files
* removed @author tags
* added a timeout test & fixed the sampler to be more consistent/correct
* There was already an expected exception test case

Thanks for looking the patch over.
Comment 11 Sebb 2009-09-21 13:59:15 UTC
Thanks very much for the patches.

It was reasonably easy to merge the JUnit4 code with the existing JUnit3 code, so I just added a checkbox to the GUI to allow users to select JUnit4 rather than JUnit3 test cases. I think this is easier than having two separate samplers.

SVN checkins:

Bugzilla 47803 - Support for JUnit 4.x tests defined by annotations
Merge code from JUnitAnnotatedSampler.java

URL: http://svn.apache.org/viewvc?rev=817404&view=rev
Log:
Bugzilla 47803 - Support for JUnit 4.x tests defined by annotations
Fix bug: test element not updated if method/classname not defined

URL: http://svn.apache.org/viewvc?rev=817397&view=rev
Log:
Bugzilla 47803 - Support for JUnit 4.x tests defined by annotations
Sample test cases

URL: http://svn.apache.org/viewvc?rev=816976&view=rev
Log:
Tidy up.
Add hooks for JUnit4 GUI support
Comment 12 The ASF infrastructure team 2022-09-24 20:37:43 UTC
This issue has been migrated to GitHub: https://github.com/apache/jmeter/issues/2273