使用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
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)
(1)覆盖beforeTestClass(TextContext testContext):
import org.springframework.test.context.TestContext; …Run Code Online (Sandbox Code Playgroud) 我有一个需要等待一段时间的应用程序.它必须等到服务器填充几个数据字段.
服务器的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)