PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/content/server/framework/atlassian-sdk/create-and-run-unit-tests.md

https://bitbucket.org/zchristmas/atlassian-sdk-docs
Markdown | 231 lines | 177 code | 54 blank | 0 comment | 0 complexity | 1ed72e51e54c6b48d6e8571dd5f3f9dd MD5 | raw file
Possible License(s): LGPL-2.0
  1. ---
  2. aliases:
  3. - /server/framework/atlassian-sdk/create-and-run-unit-tests-15335878.html
  4. - /server/framework/atlassian-sdk/create-and-run-unit-tests-15335878.md
  5. category: devguide
  6. confluence_id: 15335878
  7. dac_edit_link: https://developer.atlassian.com/pages/editpage.action?cjm=wozere&pageId=15335878
  8. dac_view_link: https://developer.atlassian.com/pages/viewpage.action?cjm=wozere&pageId=15335878
  9. date: '2017-12-08'
  10. guides: tutorials
  11. legacy_title: Create and Run Unit Tests
  12. platform: server
  13. product: atlassian-sdk
  14. subcategory: learning
  15. title: Create and run unit tests
  16. ---
  17. # Create and run unit tests
  18. This page explains how to create and run unit tests in a plugin.  You should you have already worked through [Generate and Examine Skeleton Tests](/server/framework/atlassian-sdk/generate-and-examine-skeleton-tests).   The Atlassian Plugin Framework requires that you use JUnit 4.10 or higher. By default, the SDK 4.1 (and higher) `atlas-` commands all generate a `pom.xml` file with a dependency on the appropriate JUnit version. If you are working with an older plugin, you should make sure to update JUnit `<version>` value to 4.10 before continuing.  
  19. ## JUnit Quick Reminders
  20. You use JUnit to annotate test methods in your plugin. These annotations instruct and inform the JUnit framework how to interpret your test. The following table provides a short refresher of some common JUnit annotations:
  21. | Annotation Type | Description |
  22. |-----------------|------------------------------------------------------------------------------------------------------------|
  23. | `@After` | Release external resources allocated in the `@Before` method. |
  24. | `@AfterClass` | Release expensive external resources shared among tests. These are allocated in the `@BeforeClass` method. |
  25. | `@Before` | Create resources or prepares an environment before each `@Test`. |
  26. | `@BeforeClass` | Performs expensive environment setup or creates expensive resources shared among tests. |
  27. | `@Ignore` | Ignore the `@Test` method. |
  28. | `@Test` | Identifies a test method. |
  29. Recall that JUnit tests can run in any order.  In fact, the order varies across different Java versions. So, remember to keep each unit test independent of another.  If your tests are dependent, and they run in a different order than you expect, you may find it difficult to debug test failures.  Test failure is another reason to keep your tests independent. If your tests are dependent, the failure of one can cause the remaining tests to fail. 
  30. ## Step 1.  Run the Generated MyComponentUnitTest 
  31. When you created your plugin, the system generated a skeleton `MyPluginComponent.java` class.  This class has a `getName()` method whose implementation relies on the Shared Access Layer (SAL)  `com.atlassian.sal.api.ApplicationProperties` API.  You'll find a unit test of this method in your `MyComponentUnitTest.java` class:
  32. ``` java
  33. @Test
  34. public void testMyName()
  35. {
  36. MyPluginComponent component = new MyPluginComponentImpl(null);
  37. assertEquals("names do not match!", "myComponent",component.getName());
  38. }
  39. ```
  40. When you next run your plugin with `atlas-run`, the command also runs your unit tests.  Often, you only want to run your unit tests, you use the `atlas-unit-test` command to do this. The command executes only the `test` scope defined by the project `pom.xml` file. Try the `atlas-unit-test` command now:
  41. 1. Go to a command line.
  42. 2. Change to your plugin's top level `PLUGIN_HOME` directory.
  43. 3. Enter the `atlas-unit-test` command:
  44. ``` bash
  45. atlas-unit-test
  46. ```
  47. The command output should contain test output similar to the following:
  48. ``` bash
  49. ...
  50.  
  51. [INFO] [surefire:test]
  52. [INFO] Surefire report directory: /Users/manthony/atlastutorials/testTutorial/target/surefire-reports
  53. -------------------------------------------------------
  54. T E S T S
  55. -------------------------------------------------------
  56. Running ut.com.example.plugins.tutorial.jira.testTutorial.MyComponentUnitTest
  57. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.111 sec
  58. Results :
  59. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
  60. [INFO] ------------------------------------------------------------------------
  61. [INFO] BUILD SUCCESSFUL
  62. [INFO] ------------------------------------------------------------------------
  63. [INFO] Total time: 29 seconds
  64. [INFO] Finished at: Thu Nov 15 09:57:19 PST 2012
  65. [INFO] Final Memory: 69M/123M
  66. [INFO] ------------------------------------------------------------------------
  67. ```
  68. You can see that the generated unit test was successful. 
  69. ## Step 2.  Create a Failing Unit Test
  70. The build fails when one of your tests fails. Let's see how this work by adding a failling file. If you haven't already done so, start Eclipse and open the `testTutorial` you created.
  71. 1. Edit the `PLUGIN_HOME/src/main/java/com/atlassian/plugins/tutorials/jira/testTutorial/MyPluginComponent.java` file.
  72. 2. Add a new method to the file:
  73. ``` java
  74. int addNumbers(int x, int y);
  75. ```
  76. 3. Edit the `PLUGIN_HOME/src/main/java/com/atlassian/plugins/tutorials/jira/testTutorial/MyPluginComponentImpl.java` file.
  77. 4. Add the following code to the file:
  78. ``` java
  79. @Override
  80. public int addNumbers(int x, int y) {
  81. return x + y;
  82. }
  83. ```
  84. The plugin method adds two numbers together.
  85. 5. Save and close both files.
  86. 6. Locate the `PLUGIN_HOME/src/test/java/ut/com/atlassian/plugins/tutorials/jira/testTutorial/MyComponentUnitTest.java` file.
  87. 7. Edit the file and add the following unit test code:
  88. ``` java
  89. @Test
  90. public void testAdd()
  91. {
  92. MyPluginComponent component = new MyPluginComponentImpl(null);
  93. assertEquals("Result", 8, component.addNumbers(8, 8));
  94. }
  95. ```
  96. You should now have one passing test and a new one that should fail.
  97. 8. Close and save the file.
  98. 9. Go to a command line.
  99. 10. Make sure you are in at the top level `PLUGIN_HOME` directory for you plugin.
  100. 11. Enter the `atlas-unit-test` command.
  101. The failing test returns output similar to the following:
  102. ``` bash
  103. Failed tests: testAdd(ut.com.example.plugins.tutorial.jira.testTutorial.MyComponentUnitTest): Result expected:<8> but was:<16>
  104. ```
  105. With a failing test, the Maven build reports an error.  If you were to run `atlas-run` at this point, it would fail also because you have a build error. If you have some time, try the `atlas-run` command.
  106. ## Step 3. Surefire Reports for  Your Tests
  107. The Atlassian Plugin SDK uses the Maven Surefire Plugin during the `test` phase to run your tests. Running `atlas-unit-test` creates a `PLUGIN_HOME/target/test-classes` directory containing your compiled test. Surefire also creates a `PLUGIN_HOME/target/surefire` directory to hold temporary files during the test generation. After the test phase completes, Surefire generates a  `PLUGIN_HOME/target/surefire-reports` directory.   
  108. Take a moment and examine the Surefire report generated by your testing:
  109. 1. Change directory to `PLUGIN_HOME/target/surefire-reports` directory.
  110. 2. List the directory contents.
  111. You should see something similar to the following:
  112. ``` bash
  113. TEST-ut.com.example.plugins.tutorial.jira.testTutorial.MyComponentUnitTest.xml ut.com.example.plugins.tutorial.jira.testTutorial.MyComponentUnitTest.txt
  114. ```
  115. Both files are Surefire report files.  Each time your tests you overwrite these files.
  116. 3. Take a minute and review the contents of each file.
  117. The XML report includes a set of environmental properties used in the test.  
  118. The `@Ignore` annotation is useful if you want to skip a test you know is obsolete or you just aren't ready to use.  Sometimes, during development, you may encounter tests that fail for other reasons.  Remember the `atlas-` commands that start a host application with you plugin installed, also runs your tests. Failing test are inconvient if you just want to do `atlas-run` and see your test in a host application. Go ahead and ignore the failing test in  your sample code:
  119. 1. Edit the `MyComponentUnitTest.java`.
  120. 2. Add an import for the JUnit @Ignore annotation.
  121. ``` bash
  122. import org.junit.Ignore;
  123. ```
  124. 3. Add the `@Ignore` annotation to the `testAdd()` method:
  125. ``` java
  126. @Ignore
  127. @Test
  128. public void testAdd()
  129. {
  130. MyPluginComponent component = new MyPluginComponentImpl(null);
  131. assertEquals("Result", 8, component.addNumbers(8, 8));
  132. }
  133. ```
  134. 4. Save and close the file.
  135. 5. Rerun your unit tests.
  136. You should see that only a single test is run, the first, the framework skips the other:
  137. ``` bash
  138. [INFO] [surefire:test]
  139. [INFO] Surefire report directory: /Users/manthony/atlastutorials/testTutorial/target/surefire-reports
  140. -------------------------------------------------------
  141. T E S T S
  142. -------------------------------------------------------
  143. Running ut.com.example.plugins.tutorial.jira.testTutorial.MyComponentUnitTest
  144. Tests run: 2, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.09 sec
  145. Results :
  146. Tests run: 2, Failures: 0, Errors: 0, Skipped: 1
  147. [INFO] ------------------------------------------------------------------------
  148. [INFO] BUILD SUCCESSFUL
  149. [INFO] ------------------------------------------------------------------------
  150. [INFO] Total time: 28 seconds
  151. [INFO] Finished at: Thu Nov 15 10:34:32 PST 2012
  152. [INFO] Final Memory: 69M/123M
  153. [INFO] ------------------------------------------------------------------------
  154. ```
  155. ## Step 4. Alternative Ways to Run or to Skip Tests
  156. Both Eclipse and IDEA allow you to run JUnit tests from within your IDE.  If you are following along with this tutorial exactly, you should be using Eclipse. Go ahead and try this now:
  157. 1. Edit the `PLUGIN_HOME/src/test/java/ut/com/atlassian/plugins/tutorials/jira/testTutorial/MyComponentUnitTest.java` file.
  158. 2. Remove the `@Ignore` annotation you added in the previous procedure.
  159. 3. Save the file.
  160. 4. Select **Run As &gt; JUnit** Test.
  161. You should see output similar to the following:
  162. <img src="/server/framework/atlassian-sdk/images/junit-fails.png" width="700" />
  163.  Attempt an `atlas-run` now:
  164. 1. Go to the command line.
  165. 2. Change to your `PLUGIN_HOME` directory:
  166. 3. Enter the `atlas-run` command:
  167. ``` bash
  168. atlas-run
  169. ```
  170. Very quickly the `testAdd()` method fails, the build does not complete, and `atlas-run` exits without launching the host application.
  171. 4. Rather than editing your tests to fix the failure, which can be time consuming when you many tests, skip all tests from the command line by entering the following:
  172. ``` bash
  173. atlas-run -DskipTests=true
  174. ```
  175. It may have crossed your mind that skipping tests in general may be a good idea if you just want to run the host application and your project has a lot of tests!
  176. ## Next Steps
  177. In the next section, you learn about the [tools and processes for running traditional integration tests](/server/framework/atlassian-sdk/create-and-run-traditional-integration-tests).