code_junkie

Before and After Suite execution hook in jUnit 4.x

Nov 10th, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. package com.test;
  2.  
  3. import org.junit.AfterClass;
  4. import org.junit.BeforeClass;
  5. import org.junit.runner.RunWith;
  6. import org.junit.runners.Suite;
  7. import org.junit.runners.Suite.SuiteClasses;
  8.  
  9. @RunWith(Suite.class)
  10. @SuiteClasses({Test1.class, Test2.class})
  11. public class TestSuite {
  12.  
  13. @BeforeClass
  14. public static void setUp() {
  15. System.out.println("setting up");
  16. }
  17.  
  18. @AfterClass
  19. public static void tearDown() {
  20. System.out.println("tearing down");
  21. }
  22.  
  23. }
  24.  
  25. package com.test;
  26.  
  27. import org.junit.Test;
  28.  
  29.  
  30. public class Test1 {
  31. @Test
  32. public void test1() {
  33. System.out.println("test1");
  34. }
  35.  
  36. }
  37.  
  38. setting up
  39. test1
  40. test2
  41. tearing down
  42.  
  43. <plugin>
  44. <groupId>org.apache.maven.plugins</groupId>
  45. <artifactId>maven-surefire-plugin</artifactId>
  46. <version>2.5</version>
  47. <configuration>
  48. <includes>
  49. <include>**/*Suite.java</include>
  50. </includes>
  51. <excludes>
  52. <exclude>**/*Test.java</exclude>
  53. <exclude>**/*Tests.java</exclude>
  54. </excludes>
  55. </configuration>
  56. </plugin>
  57.  
  58. import junit.framework.Test;
  59. import junit.framework.TestResult;
  60. import junit.framework.TestSuite;
  61.  
  62. public class AllTests {
  63. public static Test suite() {
  64. TestSuite suite = new TestSuite("TestEverything");
  65. //$JUnit-BEGIN$
  66. suite.addTestSuite(TestOne.class);
  67. suite.addTestSuite(TestTwo.class);
  68. suite.addTestSuite(TestThree.class);
  69. //$JUnit-END$
  70. }
  71.  
  72. public static void main(String[] args)
  73. {
  74. AllTests test = new AllTests();
  75. Test testCase = test.suite();
  76. TestResult result = new TestResult();
  77. setUp();
  78. testCase.run(result);
  79. tearDown();
  80. }
  81. public void setUp() {}
  82. public void tearDown() {}
  83. }
  84.  
  85. public class AbstractTest {
  86. private static int nbTests = listClassesIn(<package>).size();
  87. private static int curTest = 0;
  88.  
  89. @BeforeClass
  90. public static void incCurTest() { curTest++; }
  91.  
  92. @AfterClass
  93. public static void closeTestSuite() {
  94. if (curTest == nbTests) { /*cleaning*/ }
  95. }
  96. }
  97.  
  98. public class Test1 extends AbstractTest {
  99. @Test
  100. public void check() {}
  101. }
  102. public class Test2 extends AbstractTest {
  103. @Test
  104. public void check() {}
  105. }
  106.  
  107. import org.junit.*;
  108. import static org.junit.Assert.*;
  109. import java.util.*;
  110.  
  111. class SomethingUnitTest {
  112. @BeforeClass
  113. public void runBeforeClass()
  114. {
  115.  
  116. }
  117.  
  118. @AfterClass
  119. public void runAfterClass()
  120. {
  121.  
  122. }
  123.  
  124. @Before
  125. public void setUp()
  126. {
  127.  
  128. }
  129.  
  130. @After
  131. public void tearDown()
  132. {
  133.  
  134. }
  135.  
  136. @Test
  137. public void testSomethingOrOther()
  138. {
  139.  
  140. }
  141.  
  142. }
Add Comment
Please, Sign In to add comment