Junit+Spring测试

//1.首先定义一个公用的抽像类,继承已实现的spring junit类
public abstract class AbstractTest extends AbstractDependencyInjectionSpringContextTests {
	public AbstractTest(String name) {
		super(name);
	}
	public AbstractTest() {
		super();
	}
    protected String[] getConfigLocations() {
        return new String[]{"file:src/main/webapp/WEB-INF/applicationContext*.xml"};
    }
    
}

//2.定义一个测试类,继承公用抽像测试类
public class CommunityManagerTest extends AbstractTest { 
	
	private CommunityManager communityManager;
	
	public void setCommunityManager(CommunityManager communityManager) {
		this.communityManager = communityManager;
	}
	
	public void test() { 
		
		List<UserFriends> t =  communityManager.findUserFriendList(2L, 1, 10);
		for (UserFriends userFriends : t) {
			Muser u = userFriends.getMuser();
			System.out.println(u);
		}
		
	}
	

}

 

  

//3.最后定义一个测试类
public class Test {
	public static void main(String[] args) throws Throwable {
		/**
		 * 1、测试某个类的所有方法
		 */
		//TestRunner.run(CommunityManagerTest.class);//第一种方式
		
		//TestSuite suite = new TestSuite("TestSuite");//第二种方式
		//suite.addTestSuite(CommunityManagerTest.class);
		//junit.textui.TestRunner.run(suite);
		
		/**
		 * 2、测试多个类的所有方法
		 */
		//TestSuite suite = new TestSuite("TestSuite");
		//suite.addTestSuite(CommunityManagerTest.class);
		//suite.addTestSuite(CommunityManagerTest.class);
		//junit.textui.TestRunner.run(suite);
		
		/**
		 * 3、测试某个类的某个方法
		 */
		//TestSuite suite = new TestSuite("TestSuite");
		//suite.addTest(new CommunityManagerTest("testCommunity3"));
		//suite.addTest(new CommunityManagerTest("testCommunity4"));
		//junit.textui.TestRunner.run(suite);
		
		/**
		 * 4、测试多个类的某个方法
		 */
		//TestSuite suite = new TestSuite("TestSuite");
		//suite.addTest(new CommunityManagerTest("testCommunity"));
		//suite.addTest(new CommunityManagerTest("testCommunity2"));
		//suite.addTest(new CommunityManagerTest("testCommunity3"));
		//suite.addTest(new CommunityManagerTest("testCommunity4"));
		//junit.textui.TestRunner.run(suite);
	}
}

 

你可能感兴趣的:(spring,Web,xml,JUnit)