[rules-dev] Test cases upgraded from JUnit 3 to JUnit 4 (regexes)

Geoffrey De Smet ge0ffrey.spam at gmail.com
Sun Dec 26 14:48:13 EST 2010


BTW, if you're looking the do a JUnit 3 to 4 migration on any other code 
you got lying around, here are the regular expressions I used:


// Add @Test
Replace:
^[ \t]+(public +void +test)
With:
     @Test\n    $1
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java

// Remove double @Test's on already @Test annotated files
Replace:
^[ \t]+ at Test\n[ \t]+ at Test
With:
     @Test
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java


// Remove all empty setUp's
Replace:
^[ \*]+((public|protected) +)?void 
+setUp\(\)[^\{]*\{\s*(super\.setUp\(\);)?\s*\}\n([ \t]*\n)?
With nothing
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java

// Add @Before to all setUp's
Replace:
^([ \t]+ at Override\n)?[ \t]+((public|protected) +)?(void +setUp\(\))
With:
     @Before\n    public void setUp()
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java

// Remove double @Before's on already @Before annotated files
Replace:
^[ \t]+ at Before\n[ \t]+ at Before
With:
     @Before
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java


// Remove all empty tearDown's
Replace:
^[ \*]+((public|protected) +)?void 
+tearDown\(\)[^\{]*\{\s*(super\.tearDown\(\);)?\s*\}\n([ \t]*\n)?
With nothing
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java

// Add @After to all tearDown's
Replace:
^([ \t]+ at Override\n)?[ \t]+((public|protected) +)?(void +tearDown\(\))
With:
     @After\n    public void tearDown()
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java

// Remove double @After's on already @After annotated files
Replace:
^[ \t]+ at After\n[ \t]+ at After
With:
     @After
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java


// Remove old imports, add new imports
Replace:
^([ \t]*import[ \t]+junit\.framework\.Assert;\n)?[ \t]*import[ 
\t]+junit\.framework\.TestCase;
With:
import org.junit.After;\nimport org.junit.Before;\nimport 
org.junit.Test;\nimport static org.junit.Assert.*;
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java


// Remove all extends TestCase
Replace:
[ \t]+extends[ \t]+TestCase[ \t]+\{
With:
  {
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java



// Look for import junit.framework;
Find:
import junit\.framework
Manually fix
Regular Expression: on
Case sensitive: on


// Look for ignored tests (FIXME, disabled, ...)
Find:
public[ \t]+void[ \t]+\w+test
Manually fix
Regular Expression: on
Case sensitive: on


// Look for dummy/empty tests
Find:
public[ \t]+void[ \t]+test[\w\d]*\(\s*\)\s*\{\s*(//[^\n]*)?\s*\}
Manually fix
Regular Expression: on
Case sensitive: on




Op 26-12-10 20:06, Geoffrey De Smet schreef:
> Hi guys,
>
> I've updated all testcases on master to use JUnit 4 style
> (except for drools-eclipse and the non-active parts of drools-grid).
> The main reason is so we now let hudson report how many testcases are 
> ignored (annoted with @Ignore instead of disabled).
>
> Some notes:
>
>     * *Don't disable testcases by prefixing them with FIXME_ or
>       disabled_ or by commenting out @Test*
>           o *Instead, add the @Ignore annotation*
>                 + This way hudson gives us a report on how many
>                   testcases have been ignored.
>     * *Don't use try-catch to test if an exception is thrown. Use
>       @Test(expected = MyException.class)*
>           o I haven't migrated this in all testcases, as I couldn't
>             automate that.
>           o Please fix it in any code you come across.
>     * To use assertions like assertEquals(a,b), just add this import:
>           o import static org.junit.Assert.*;
>     * Don't import anything from junit.framework.*.
>           o Import only from org.junit.*
>           o Configure your Eclipse/Intellij to not propose those
>             imports to keep mistakes to a minimum.
>     * TestSuites have been removed.
>           o Use your Eclipse/IntelliJ to single out tests, packages of
>             tests or modules.
>           o They bring extra maintenance (and seem to be seen as
>             legacy since junit 4?)
>     * Testcases that print out their own name, no longer print out
>       their own name
>           o The junit runner prints out the name already
>                 + Otherwise it looks like it's run twice (while it's not)
>
> I hope this push (many files were changed) doesn't cause to many merge 
> conflicts.
> If it does: just take your revision and upgrade your file to junit 4 
> manually.
>
> Learn more:
> - Junit 4 in 60 seconds: 
> http://www.cavdar.net/2008/07/21/junit-4-in-60-seconds/
> -- 
> With kind regards,
> Geoffrey De Smet
>
>
> _______________________________________________
> rules-dev mailing list
> rules-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-dev

-- 
With kind regards,
Geoffrey De Smet

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-dev/attachments/20101226/a1e813c4/attachment.html 


More information about the rules-dev mailing list