- package com.ohergal.gtest;
- /**
- * Created by IntelliJ IDEA.
- * User: Administrator
- * Date: 2008-5-27
- * Time: 11:30:29
- * To change this template use File | Settings | File Templates.
- */
- public interface IMyService {
- public boolean vilidateMySelf(String s);
- }
- package com.ohergal.gtest;
- /**
- * Created by IntelliJ IDEA.
- * User: Administrator
- * Date: 2008-5-27
- * Time: 11:31:39
- * To change this template use File | Settings | File Templates.
- */
- public class MyServiceImp implements IMyService{
- public boolean vilidateMySelf(String s){
- System.out.println("comto test");
- return true;
- }
- }
- package com.ohergal.gtest;
- import com.google.inject.Inject;
- /**
- * Created by IntelliJ IDEA.
- * User: Administrator
- * Date: 2008-5-27
- * Time: 14:17:11
- * To change this template use File | Settings | File Templates.
- */
- public class MyApp {
- private IMyService service = null;
- @Inject
- public void setService(IMyService service){
- this.service=service;
- }
- public boolean go(){
- return service.vilidateMySelf("test");
- }
- }
- package com.ohergal.gtest;
- import com.google.inject.Module;
- import com.google.inject.Binder;
- import com.google.inject.Scopes;
- /**
- * Created by IntelliJ IDEA.
- * User: Administrator
- * Date: 2008-5-27
- * Time: 11:39:37
- * To change this template use File | Settings | File Templates.
- */
- public class MyModule implements Module {
- public void configure(Binder binder) {
- binder.bind(IMyService.class)
- .to(MyServiceImp.class)
- .in(Scopes.SINGLETON);
- }
- }
单元测试代码
- package com.ohergal.gtest;
- import junit.framework.Test;
- import junit.framework.TestCase;
- import junit.framework.TestSuite;
- import com.google.inject.Module;
- import com.google.inject.Injector;
- import com.google.inject.Guice;
- /**
- * Unit test for simple App.
- */
- public class AppTest
- extends TestCase
- {
- /**
- * Create the test case
- *
- * @param testName name of the test case
- */
- public AppTest( String testName )
- {
- super( testName );
- }
- /**
- * @return the suite of tests being tested
- */
- public static Test suite()
- {
- return new TestSuite( AppTest.class );
- }
- /**
- * Rigourous Test :-)
- */
- public void testApp()
- {
- MyApp app = new MyApp();
- Module module = new MyModule();
- Injector in = Guice.createInjector(module);
- in.injectMembers(app);
- //assertTrue( true );
- assertTrue(app.go());
- }
- }