JUnit4 和 Guice 測試庫:Acai
Acai 是 JUnit4 和 Guice 的測試庫,可以更容易的編寫應用功能測試。
主要特性:
-
注入測試需要的助手類
-
啟動測試需要的任意的服務
-
運行測試之間的服務清理
-
按照正確順序啟動多個服務
-
創建測試作用域綁定
Acai 主要針對的是應用大型功能測試。
安裝
<dependency> <groupId>com.google.acai</groupId> <artifactId>acai</artifactId> <version>0.1</version> <scope>test</scope> </dependency>
使用 Acai 進行測試注入
@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 啟動服務
@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(); } } }
本文由用戶 byb1234 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!