SpringBoot+MyBatis框架的项目,如何进行集成测试?

**@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(
classes = Application.class,
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT
)
@DirtiesContext
public class dataOpTest {

private TBookController tBookController = new TBookController();
public TBookMapper tBookMapper;

@Test
public void testIfExistByName(){
    String bk_name = "c++";
    //int existCount = tBookController.ifExist(bk_name);
    int result = tBookMapper.existByNameAndState(bk_name);
    Assert.assertEquals(result, 1);
}

}**
以上代码,会认为tBookController是空指针,它本身是一个接口,sql实现都在xml文件里面,但是集成测试的时候怎么做?

可以用MockMvc,具体请参考http://blog.csdn.net/xiao_xuwen/article/details/52890730

tBookController是空指针???不是吧,,,你后面都new了,,,

tBookMapper的空指针吧

加个注解试试”@Autowired “,,把new去了
两个都加上试试

@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration("generatorConfig.xml")
public class MybatisOpTest {

@Mock
private TBookController tBookController = new TBookController();

    ……
}


在TBookController上面加上一个@Mock注解即可,

在测试类上加上:

@RunWith(MockitoJUnitRunner.class)即可