code_junkie

Best way to automagically migrate tests from JUnit 3 to JUnit 4?

Nov 12th, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. " extends TestCase"
  2.  
  3. @Override
  4. protected void setUp() throws Exception { }
  5.  
  6. @Before
  7. protected void setUp() throws Exception { }
  8.  
  9. @Override
  10. protected void tearDown() throws Exception { }
  11.  
  12. @After
  13. protected void tearDown() throws Exception { }
  14.  
  15. // Add @Test
  16. Replace:
  17. ^[ t]+(public +void +test)
  18. With:
  19. @Testn $1
  20. Regular Expression: on
  21. Case sensitive: on
  22. File name filter:
  23. *Test.java
  24.  
  25. // Remove double @Test's on already @Test annotated files
  26. Replace:
  27. ^[ t]+@Testn[ t]+@Test
  28. With:
  29. @Test
  30. Regular Expression: on
  31. Case sensitive: on
  32. File name filter:
  33. *Test.java
  34.  
  35.  
  36. // Remove all empty setUp's
  37. Replace:
  38. ^[ *]+((public|protected) +)?void +setUp()[^{]*{s*(super.setUp();)?s*}n([ t]*n)?
  39. With nothing
  40. Regular Expression: on
  41. Case sensitive: on
  42. File name filter:
  43. *Test.java
  44.  
  45. // Add @Before to all setUp's
  46. Replace:
  47. ^([ t]+@Overriden)?[ t]+((public|protected) +)?(void +setUp())
  48. With:
  49. @Beforen public void setUp()
  50. Regular Expression: on
  51. Case sensitive: on
  52. File name filter:
  53. *Test.java
  54.  
  55. // Remove double @Before's on already @Before annotated files
  56. Replace:
  57. ^[ t]+@Beforen[ t]+@Before
  58. With:
  59. @Before
  60. Regular Expression: on
  61. Case sensitive: on
  62. File name filter:
  63. *Test.java
  64.  
  65.  
  66. // Remove all empty tearDown's
  67. Replace:
  68. ^[ *]+((public|protected) +)?void +tearDown()[^{]*{s*(super.tearDown();)?s*}n([ t]*n)?
  69. With nothing
  70. Regular Expression: on
  71. Case sensitive: on
  72. File name filter:
  73. *Test.java
  74.  
  75. // Add @After to all tearDown's
  76. Replace:
  77. ^([ t]+@Overriden)?[ t]+((public|protected) +)?(void +tearDown())
  78. With:
  79. @Aftern public void tearDown()
  80. Regular Expression: on
  81. Case sensitive: on
  82. File name filter:
  83. *Test.java
  84.  
  85. // Remove double @After's on already @After annotated files
  86. Replace:
  87. ^[ t]+@Aftern[ t]+@After
  88. With:
  89. @After
  90. Regular Expression: on
  91. Case sensitive: on
  92. File name filter:
  93. *Test.java
  94.  
  95.  
  96. // Remove old imports, add new imports
  97. Replace:
  98. ^([ t]*import[ t]+junit.framework.Assert;n)?[ t]*import[ t]+junit.framework.TestCase;
  99. With:
  100. import org.junit.After;nimport org.junit.Before;nimport org.junit.Test;nimport static org.junit.Assert.*;
  101. Regular Expression: on
  102. Case sensitive: on
  103. File name filter:
  104. *Test.java
  105.  
  106.  
  107. // Remove all extends TestCase
  108. Replace:
  109. [ t]+extends[ t]+TestCase[ t]+{
  110. With:
  111. {
  112. Regular Expression: on
  113. Case sensitive: on
  114. File name filter:
  115. *Test.java
  116.  
  117.  
  118.  
  119. // Look for import junit.framework;
  120. Find:
  121. import junit.framework
  122. Manually fix
  123. Regular Expression: on
  124. Case sensitive: on
  125.  
  126.  
  127. // Look for ignored tests (FIXME, disabled, ...)
  128. Find:
  129. public[ t]+void[ t]+w+test
  130. Manually fix
  131. Regular Expression: on
  132. Case sensitive: on
  133.  
  134.  
  135. // Look for dummy/empty tests
  136. Find:
  137. public[ t]+void[ t]+test[wd]*(s*)s*{s*(//[^n]*)?s*}
  138. Manually fix
  139. Regular Expression: on
  140. Case sensitive: on
  141.  
  142. public void test
  143. @Testn public void test
  144.  
  145. @Overriden.*protected void onSetUp
  146. @Beforen protected void onSetUp
  147.  
  148. @Overriden.*protected void onTearDown
  149. @Aftern protected void onTearDown
Add Comment
Please, Sign In to add comment