JUnit与Spring Test、Mockito配合使用 1. 项目依赖

org.springframework spring-test test 4.2.5.RELEASE junit junit 4.12 test

2. 创建测试类

@RunWith(SpringJUnit4ClassRunner.class)
//指定配置文件
@ContextConfiguration(locations = {“/spring/stock-session-factory.xml”})
//指定资源文件
@TestPropertySource(locations = {“/application.properties”})
public class StockTest {

@Autowired
private StockMapper stockMapper;

@Transactional
@Test
public void test(){
    Stock stock = new Stock();
    stock.setCode("900001");
    stock.setName("tcl");
    stockMapper.insert(stock);
}

}

3. Mock 3.1 加入依赖Mockito

org.mockito mockito-all 1.10.19 test

3.2 Mock数据库访问操作

@RunWith(SpringJUnit4ClassRunner.class)
//指定配置文件
@ContextConfiguration(locations = {“/spring/stock-session-factory.xml”})
//指定资源文件
@TestPropertySource(locations = {“/application.properties”})
public class StockTest {

@InjectMocks
@Autowired
private StockService stockService;
@Mock
private StockMapper stockMapperMocker;

@Before
public void setup(){
    MockitoAnnotations.initMocks(this);
}

@Transactional
@Test
public void test(){
    Stock stock = new Stock();
    stock.setName("露露");

        //如果是数据库插入操作,则直接跳过,返回1
when(stockMapperMocker.insert(any(Stock.class))).thenReturn(1);
        //如果是数据库查询操作,则直接返回stock对象
when(stockMapperMocker.selectByPrimaryKey(anyString())).thenReturn(stock);

    stockService.insert(null);
    String name = stockService.selectByCode("000848");
    System.out.println( name );
}

}

项目源码 Github