小编ral*_*son的帖子

@BeforeClass和Spring @TestExecutionListener beforeTestClass()之间有什么区别

使用JUnit @BeforeClass和Spring @TestExecutionListener beforeTestClass(TestContext testContext)"hook"之间有什么区别?如果有差异,在哪种情况下使用哪一个?

Maven依赖:
spring-core:3.0.6.RELEASE
spring-context:3.0.6.RELEASE
spring-test:3.0.6.RELEASE
spring-data-commons-core:1.2.0.M1
spring-data-mongodb:1.0 .0.M4
mongo-java-driver:2.7.3
junit:4.9
cglib:2.2

使用JUnit @BeforeClass注释:

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

@ContextConfiguration(locations = { "classpath:test-config.xml" })
public class TestNothing extends AbstractJUnit4SpringContextTests {

    @Autowired
    PersonRepository repo;

    @BeforeClass
    public static void runBefore() {
        System.out.println("@BeforeClass: set up.");
    }

    @Test
    public void testInit() {
        Assert.assertTrue(repo.findAll().size() == 0 );
    }
}

=> @BeforeClass: set up.
=> Process finished with exit code 0
Run Code Online (Sandbox Code Playgroud)

使用Spring钩子:

(1)覆盖beforeTestClass(TextContext testContext):

import org.springframework.test.context.TestContext; …
Run Code Online (Sandbox Code Playgroud)

spring-test junit4

9
推荐指数
1
解决办法
8417
查看次数

在Java中等待的最佳方式

我有一个需要等待一段时间的应用程序.它必须等到服务器填充几个数据字段.

服务器的API为我提供了一种请求数据的方法,很简单......

服务器的API还提供了一种接收我的数据的方法,一次一个字段.它并没有告诉我何时完成所有字段的填充.

在我的请求完成服务器处理之前,最有效的方法是什么?这是一些伪代码:

public class ServerRequestMethods {
    public void requestData();
}

public interface ServerDeliveryMethods {
    public void receiveData(String field, int value);
}

public class MyApp extends ServerRequestMethods implements ServerDeliveryMethods {
    //store data fields and their respective values
    public Hashtable<String, Integer> myData;    

    //implement required ServerDeliveryMethods
    public void receiveData(String field, int value) {
        myData.put(field, value);    
    }

    public static void main(String[] args) {
        this.requestData();

        // Now I have to wait for all of the fields to be populated,
        // so that I …
Run Code Online (Sandbox Code Playgroud)

java notify wait

4
推荐指数
1
解决办法
8437
查看次数

标签 统计

java ×1

junit4 ×1

notify ×1

spring-test ×1

wait ×1