Spring单元测试

mapper

package dao;

import com.dao.WzmlMapper;
import com.util.Tree;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"})
public class WzmlMapperTest {
    @Autowired
    private WzmlMapper wzmlMapper;
    @Test
    public void test(){
        List<Tree> list = wzmlMapper.selectParent();
        for (Tree tree : list){
            System.out.println(tree);
        }
    }
}

配置说明

  • 需要导入junit
  • 需要导入Spring-test 包
  • RunWith
    @RunWith(SpringJUnit4ClassRunner.class)
  • ContextConfiguration 指定配置文件,可以同时配置多个路径
    @ContextConfiguration(locations = {“classpath:spring.xml”})

常见错误

  • java.lang.Exception: No runnable methods
    方法缺少 @Test…
阅读更多

没有更多推荐了,返回首页