Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I can see several reasons for doing this, especially when using JUnit to run functional tests or test persistent objects. For example, consider an object <code>Article</code> which is persisted to some kind of persistent storage. If I would like to test the insert, update and delete functionality on the <code>Article</code> object following the unit test principle "all tests should be reorderable and test only a specific part of the functionality", I would have three tests:</p> <ul> <li><code>testInsertArticle()</code></li> <li><code>testUpdateArticle()</code></li> <li><code>testDeleteArticle()</code></li> </ul> <p>However, to be able to test the update functionality, I would first need to insert the article. To test the delete functionality, I would also need to insert an article. So, in practice, the insert functionality is already tested both in <code>testUpdateArticle()</code> and <code>testDeleteArticle()</code>. It is then tempting to just create a test method <code>testArticleFunctionality()</code> which does it all, but methods like that will eventually get huge (and they won't just test part of the functionality of the <code>Article</code> object).</p> <p>The same goes for running functional tests against for example a restful API. JUnit is great also for these cases if it wasn't for the undeterministic ordering of tests.</p> <p>That said, I extended Michael D's <code>OrderedRunner</code> to use annotations to determine order of tests, just thought I should share. It can be extended further, for example by specifying exactly which tests each test depends on, but this is what I'm using for now.</p> <p>This is how it is used. It avoids the need for naming tests like <code>AA_testInsert()</code>, <code>AB_testUpdate()</code>, <code>AC_testDelete()</code>, ..., <code>ZC_testFilter()</code>, etc. </p> <pre><code>@RunWith(OrderedRunner.class) public class SomethingTest { @Test @Order(order=2) public void testUpdateArticle() { // test update } @Test @Order(order=1) public void testInsertArticle() { // test insert } @Test @Order(order=3) public void testDeleteArticle() { // test delete } } </code></pre> <p>No matter how these tests are placed in the file, they will always be run as <code>order=1</code> first, <code>order=2</code> second and last <code>order=3</code>, no matter if you run them from inside Eclipse, using Ant, or any other way.</p> <p>Implementation follows. First, the annotation <code>Order</code>.</p> <pre><code>@Retention(RetentionPolicy.RUNTIME) public @interface Order { public int order(); } </code></pre> <p>Then, the modified <code>OrderedRunner</code>.</p> <pre><code>public class OrderedRunner extends BlockJUnit4ClassRunner { public OrderedRunner(Class&lt;?&gt; klass) throws InitializationError { super(klass); } @Override protected List&lt;FrameworkMethod&gt; computeTestMethods() { List&lt;FrameworkMethod&gt; list = super.computeTestMethods(); Collections.sort(list, new Comparator&lt;FrameworkMethod&gt;() { @Override public int compare(FrameworkMethod f1, FrameworkMethod f2) { Order o1 = f1.getAnnotation(Order.class); Order o2 = f2.getAnnotation(Order.class); if (o1 == null || o2 == null) return -1; return o1.order() - o2.order(); } }); return list; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload