Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使junit测试运行方法有序_Java_Spring_Junit_Memcached - Fatal编程技术网

Java 如何使junit测试运行方法有序

Java 如何使junit测试运行方法有序,java,spring,junit,memcached,Java,Spring,Junit,Memcached,伙计们!我有一个新问题要问你。我正在使用不同的缓存管理器向缓存中添加一些数据,我面临着这个问题。我正在使用junit和spring来实现它。当我运行测试时,测试方法是随机执行的,但我需要按顺序执行它们。怎么做??以下是一些代码和控制台输出: 服务类别: @Service("HelloCache") public class CacheServiceImpl implements CacheInterface { @Autowired @Qualifier("memcachedCl

伙计们!我有一个新问题要问你。我正在使用不同的缓存管理器向缓存中添加一些数据,我面临着这个问题。我正在使用junit和spring来实现它。当我运行测试时,测试方法是随机执行的,但我需要按顺序执行它们。怎么做??以下是一些代码和控制台输出:

服务类别:

@Service("HelloCache")
public class CacheServiceImpl implements CacheInterface {
    @Autowired
    @Qualifier("memcachedClient")
    private MemcachedClient mBean;

    public void Add(String key, Object object) {
        mBean.set(key, 12, object);
    }

    public void Get(String key) {
        mBean.get(key);
    }

    public void Delete(String key) {
        mBean.delete(key);
    }    
}
以下是测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "file:src/main/java/spring.xml")
public class UsingMemcachedTest extends TestCase {
    @Autowired
    @Qualifier("HelloCache")
    private CacheInterface emcached;
    private byte[][] i = new byte[2500][3000];
    private String key = "j";
    @Test
    public void testAddBulkObjects() {
        System.out.println("");
        System.out.println("This is async BULK adding test");
        long time = System.currentTimeMillis();
        for (int k=1; k<=1000; k++) {
            emcached.Add(key+k, i);
        }
        long time2 = System.currentTimeMillis();
        long timeE=time2-time;
        System.out.println("Vremya add BULK objects: " + timeE);
        System.out.println("");
    }

    @Test
    public void testGetBulkObjects() {
        System.out.println("");
        System.out.println("This is getting BULK objects test");
        long time = System.currentTimeMillis(); 
        for (int k=1; k<=1000; k++) {
            emcached.Get(key+k);
        }
        long time2 = System.currentTimeMillis();
        long timeE=time2-time;
        System.out.println("Vremya Get object: " + timeE);
        System.out.println("");
    } 

    @Test
    public void testDeleteBulkObjects() {
        System.out.println("");
        System.out.println("This is deleting BULK objects test");
        long time = System.currentTimeMillis();
        for (int k=1; k<=1000; k++) {
            emcached.Delete(key+k);
        }
        long time2 = System.currentTimeMillis();
        long timeE=time2-time;
        System.out.println("Vremya delete object: " + timeE);
        System.out.println("");
    }

求求你,救命!!=)

JUnit没有承诺测试的运行顺序。根据运行测试的环境和上下文,顺序会有所不同

出于这个原因(以及其他原因),排序影响测试行为被认为是非常糟糕的测试设计。您可以使用@Before go为您提供一个干净的工作环境,然后作为测试的一部分为特定测试进行任何设置


以下问题的公认答案给出了很好的解释,并链接到一些有用的资源:

JUnit 4.11添加了一些很酷的东西,允许您从版本4.11控制执行顺序。您可以使用注释和按方法名称排序来指定执行顺序:

import org.junit.Test;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MyTest {

    @Test
    public void test1Create() {
        System.out.println("first");
    }

    @Test
    public void test2Update() {
        System.out.println("second");
    }
}

为什么要按顺序执行它们?测试应该能够以任何顺序运行,依我看。你总是可以用老式的方式来做,并制作一个测试套件。我需要这样做,因为我正在向缓存中添加一些数据,然后从缓存中获取数据,然后才将其删除。这就是为什么。
import org.junit.Test;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MyTest {

    @Test
    public void test1Create() {
        System.out.println("first");
    }

    @Test
    public void test2Update() {
        System.out.println("second");
    }
}