|
功能代碼按照Spring的標準書寫,無論是通過XML配置還是通過annotation配置都可以。
測試代碼最重要的是告訴framework去哪里找bean的配置。
以Dao的Test為例:
- //告訴framework怎么運行這個類
- @RunWith(SpringJUnit4ClassRunner.class)
- //bean的配置文件路徑,這個是Test類的classpath路徑,如果是Spring推薦的目錄結構,應該在:項目目錄/src/test/resources/里
- @ContextConfiguration(locations = "classpath:app-config.xml")
- public class TestPatchDao extends AbstractTransactionalJUnit4SpringContextTests {
- //取得要測試的Dao類
- @Resource
- private PatchDao patchDao;
-
- @Before
- public void setUp() throws Exception {
- }
-
- @After
- public void tearDown() throws Exception {
- }
-
- /**
- * 測試方法
- */
- @Test
- public void testGetPatchList_1() {
- //Dao的某個方法
- List<Map<String, Object>> list = patchDao.getPatchList(1, "00C8002D00000000", 1);
- assertEquals(1, list.size());
- }
- }
再舉一個Controller的例子
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = {"classpath:app-config.xml", "classpath:mvc-config.xml"})
- public class TestMainCtrl extends AbstractTransactionalJUnit4SpringContextTests {
- @Autowired
- private MainCtrl controller;
-
- //這種方法適用于Springframework3.0,3.1換了handler的處理類。
- @Autowired
- private AnnotationMethodHandlerAdapter handlerAdapter;
-
- private final MockHttpServletRequest request = new MockHttpServletRequest();
- private final MockHttpServletResponse response = new MockHttpServletResponse();
-
- @Test
- public void testMain4User() throws Exception {
- request.setRequestURI("/main");
- request.setMethod(HttpMethod.POST.name());
- HttpSession session = request.getSession();
- //設置 認證信息
- session.setAttribute(CommonConstants.SESSION_USER_TYPE, 1);
- session.setAttribute(CommonConstants.SESSION_USER_ID, 0);
- session.setAttribute(CommonConstants.SESSION_USER_ACC, "aa1");
-
- ModelAndView mav = handlerAdapter.handle(request, response, controller);
- assertEquals("/regist", mav.getViewName());
- }
- }
TestSuite的寫法,將Test類用注解的方式配置到TestSuite類上。
- import org.junit.runner.RunWith;
- import org.junit.runners.Suite;
- import org.junit.runners.Suite.SuiteClasses;
-
- @RunWith(Suite.class)
- @SuiteClasses( { TestPatchDao.class, TestMainCtrl.class })
- public class TestSuite {}
這個工具的功能都很實用,它還可以自動生成測試代碼。測試代碼以獨立的項目存在,可以根據功能代碼的流程分支生成的測試方法,比如功能代碼里有一個if else,測試代碼就會生成3個測試方法,以便每個分支都能覆蓋到。
我們主要使用它的代碼覆蓋率功能,這個工具不但可以統(tǒng)計測試時的代碼覆蓋率,還可以統(tǒng)計debug時的覆蓋率。
1、按照安裝說明,將工具安裝進Eclipse
2、右鍵點擊項目,選擇CodePro Tools --> Instrument for Code Coverage。
再運行測試類或debug,都會出覆蓋率的統(tǒng)計。
但是統(tǒng)計覆蓋率會降低代碼運行效率,所以,不需要統(tǒng)計時再 Unistrument 就可以了。
---------------------------------------------
Springframework3.1和springsecure的controller測試類例子:
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = {"classpath:root-context.xml",
- "classpath:servlet-context.xml", "classpath:security-app-context.xml"})
- public class TestSecureCtrl extends AbstractTransactionalJUnit4SpringContextTests {
- @Autowired
- private SecureCtrl controller;
- @Autowired
- private RequestMappingHandlerAdapter handlerAdapter;
-
- private final MockHttpServletRequest request = new MockHttpServletRequest();
- private final MockHttpServletResponse response = new MockHttpServletResponse();
-
- @Test
- public void testMain4User() throws Exception {
- request.setRequestURI("/secure");
- request.setMethod(HttpMethod.POST.name());
- HttpSession session = request.getSession();
-
- //設置springsecure的內容
- List<GrantedAuthority> ga = new ArrayList<GrantedAuthority>();
- ga.add(new GrantedAuthorityImpl("ROLE_ALL"));
- User user = new User("account", "password", true, true, true, true, ga);
- SecurityContextHolder.getContext().setAuthentication(
- new UsernamePasswordAuthenticationToken(user, null));
- session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, SecurityContextHolder.getContext());
-
- ModelAndView mav = handlerAdapter.handle(request, response, new HandlerMethod(controller, "home", Model.class, HttpServletRequest.class));
-
- assertEquals("home", mav.getViewName());
- }
- }
|