`
蚂蚁搬家
  • 浏览: 1849 次
最近访客 更多访客>>
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
junit测试
package classtest;


import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import classtest.Test1.Service;
import classtest.Test1.ServiceFactory;



public class TestTest{
	private Factories fact ;
	ServiceFactory fact2;
	Service service ;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		fact = new Factories();
		service = new Service()
		{

			@Override
			public void method1() {
				// TODO Auto-generated method stub
				System.out.println("m1");
			}

			@Override
			public void method2() {
				// TODO Auto-generated method stub
				System.out.println("m2");
			}
			
		};
		fact2 = new ServiceFactory()
		{

			@Override
			public Service getService() {
				// TODO Auto-generated method stub
				return service;
			}
			
		};
	}

	@After
	public void tearDown() throws Exception {
	}
	@Test
	public void testinit()
	{
		fact.serviceConsumer(fact2);
		
		
	}
	

}
工厂方法
package classtest;

public class Test {

	   public interface Service {
			public void method1();
			public void method2();
		   }
		   public interface ServiceFactory {
			Service getService();
		   }
		   public static class Implemention1 implements Service{
			   private Implemention1(){}
			   
			public void method1(){
				System.out.println("In Implemention1 method method1()");
			}
			public void method2(){
				System.out.println("In Implemention1 method method2()");
			}
			
			public static ServiceFactory factory = new ServiceFactory(){
				public Service getService(){
					return new Implemention1();
				}
			};
		   }
		   public static class Implemention2 implements Service {
			public void method1(){
				System.out.println("in Implemention2 method method1()");
			}
			public void method2(){
				System.out.println("in Implemention2 method method2()");
			}
			
			public static ServiceFactory factory = new ServiceFactory(){
				public Service getService(){
					return new Implemention2();
				}
			};
			
		   }
		  
		
		 
}

/**
 * 
 */
package classtest;

import classtest.Test.ServiceFactory;

import classtest.Test.Service;
import classtest.Test.Implemention1;
import classtest.Test.Implemention2;

public class Factories {

	
	public static void serviceConsumer(ServiceFactory fact)
	{
		Service s = fact.getService();
		s.method1();
		s.method2();
		
	}
	public static void main(String[] args) {
		
		// TODO Auto-generated method stub
		serviceConsumer(Implemention1.factory);
		serviceConsumer(Implemention2.factory);
	}

}
Global site tag (gtag.js) - Google Analytics