Intellij idea處理Spring MVC單元測試問題

蔚藍的珊瑚海_xdcaxy2013發表於2015-12-04

若為Maven專案

1、首先得支援junit及spring-test jar包

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency>

2、進行單元測試類配置

import org.junit.runner.RunWith;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml","classpath:springmvc-servlet.xml"})

public class UserTest extends AbstractJUnit4SpringContextTests{

           //注入測試類

            @Autowired
            private IUserDao userDao;

            @Resource(name = "userDaoImpl")
             public void setUserDao(IUserDao userDao) {
                this.userDao = userDao;
            }

          @Test
          public void testDoInsert() throws Exception {

           User user = new User();

           user.setId("...............")

          ......................

          //要測試的方法

          userDao.Insert(user);

          }

}

3、右鍵執行單元測試,就OK了


相關文章