AcaiJUnit4 和 Guice 測試庫
Acai 是 JUnit4 和 Guice 的測試庫,可以更容易的編寫應(yīng)用功能測試。
主要特性:
-
注入測試需要的助手類
-
啟動測試需要的任意的服務(wù)
-
運(yùn)行測試之間的服務(wù)清理
-
按照正確順序啟動多個服務(wù)
-
創(chuàng)建測試作用域綁定
Acai 主要針對的是應(yīng)用大型功能測試。
安裝
<dependency> <groupId>com.google.acai</groupId> <artifactId>acai</artifactId> <version>0.1</version> <scope>test</scope> </dependency>
使用 Acai 進(jìn)行測試注入
@RunWith(JUnit4.class)
public class SimpleTest {
@Rule public Acai acai = new Acai(MyTestModule.class);
@Inject private MyClass foo;
@Test
public void checkSomethingWorks() {
// Use the injected value of foo here
}
private static class MyTestModule extends AbstractModule {
@Override protected void configure() {
bind(MyClass.class).to(MyClassImpl.class);
}
}
}
使用 Acai 啟動服務(wù)
@RunWith(JUnit4.class)
public class ExampleFunctionalTest {
@Rule public Acai acai = new Acai(MyTestModule.class);
@Inject private MyServerClient serverClient;
@Test
public void checkSomethingWorks() {
// Call the running server and test some behaviour here.
// Any state will be cleared by MyFakeDatabaseWiper after each
// test case.
}
private static class MyTestModule extends AbstractModule {
@Override protected void configure() {
// Normal Guice modules which configure your
// server with in-memory versions of backends.
install(MyServerModule());
install(MyFakeDatabaseModule());
install(new TestingServiceModule() {
@Override protected void configureTestingServices() {
bindTestingService(MyServerRunner.class);
bindTestingService(MyFakeDatabaseWiper.class);
}
});
}
}
private static class MyServerRunner implements TestingService {
@Inject private MyServer myServer;
@BeforeSuite void startServer() {
myServer.start().awaitStarted();
}
}
private static class MyFakeDatabaseWiper implements TestingService {
@Inject private MyFakeDatabse myFakeDatabase;
@AfterTest void wipeDatabase() {
myFakeDatabase.wipe();
}
}
}評論
圖片
表情
