Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... android.intent.action.SCREEN_ON & android.intent.action.SCREEN_OFF First, I've tried create a receiver to receive screen on/off and register receiver on AndroidManifest.xml like below, but unfortunately ... JSRequest, Get parameters from querystring with javascript in SharePoint Provides method to parse query string, filename, and pathname from URL // Initialize first JSRequest.EnsureSetup(); // Get the current fil...
Stats
JUnit with Spring : orderly test method

When using junit with spring framework for test, to sort execute order of method, will use FixMethodOrder class. It is a little inconvenience because you need to name your test method in correct way.



Execute test method ordered by method name
package com.prhythm.test;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by nanashi07 on 15/6/20.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/application-context.xml")
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OrderedTest {

    @Test
    public void test1() {
        System.out.println("test1 executed");
    }

    @Test
    public void test2() {
        System.out.println("test2 executed");
    }

    @Test
    public void test3() {
        System.out.println("test3 executed");
    }

    @Test
    public void test4() {
        System.out.println("test4 executed");
    }
}
Result:
test1 executed
test2 executed
test3 executed
test4 executed


Now there is an another way to do it.



Create a order annotation for customize order
package org.springframework.test.context.junit4;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Created by nanashi07 on 15/6/20.
 */
@Retention(RetentionPolicy.RUNTIME)
public @interface Order {
    int value();
}
Create a junit runner extends SpringJUnit4ClassRunner
package org.springframework.test.context.junit4;

import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.springframework.test.context.TestContextManager;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
public class SpringJUnit4ClassOrderedRunner extends SpringJUnit4ClassRunner {

    /**
     * Constructs a new {@code SpringJUnit4ClassRunner} and initializes a
     * {@link TestContextManager} to provide Spring testing functionality to
     * standard JUnit tests.
     *
     * @param clazz the test class to be run
     * @see #createTestContextManager(Class)
     */
    public SpringJUnit4ClassOrderedRunner(Class<?> clazz) throws InitializationError {
        super(clazz);
    }

    @Override
    protected List<FrameworkMethod> computeTestMethods() {
        List<FrameworkMethod> list = super.computeTestMethods();
        Collections.sort(list, new Comparator<FrameworkMethod>() {
            @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.value() - o2.value();
            }
        });
        return list;
    }
}
Run test as customized order
package com.prhythm.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.Order;
import org.springframework.test.context.junit4.SpringJUnit4ClassOrderedRunner;

/**
 * Created by nanashi07 on 15/6/20.
 */
@RunWith(SpringJUnit4ClassOrderedRunner.class)
@ContextConfiguration("classpath:spring/application-context.xml")
public class OrderedTest {

    @Test
    @Order(value = 4)
    public void test1() {
        System.out.println("test1 executed");
    }

    @Test
    @Order(value = 3)
    public void test2() {
        System.out.println("test2 executed");
    }

    @Test
    @Order(value = 2)
    public void test3() {
        System.out.println("test3 executed");
    }

    @Test
    @Order(value = 1)
    public void test4() {
        System.out.println("test4 executed");
    }
}
Result:
test4 executed
test3 executed
test2 executed
test1 executed