[JUnit] 使用JUnit测试SpringBoot

我只说重点

1. 首先写一个BaseTest 的测试基类。

import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public abstract  class BaseTest {
}

定义好测试基类,其他测试类直接继承测试基类就行。

测试类import org.junit.Testimport org.springframework.beans.factory.annotation.Autowired;import com.xxx.oauth.mapper.ClientMapper;

import com.xxx.oauth.model.Client;
import com.xxx.oauth2.BaseTest;
public class TestClientMappper extends BaseTest {
    @Autowired //自动注入
    ClientMapper mapper ;//这再idea中有警告,不影响运行。   如果想消除警告,请在ClientMapper中添加@Component或者@Service 或则@repository
@Test public void testClientInsert(){ Client client =new Client(); client.setClientId("uuuuuuuid"); int rows= mapper.insert(client); System.out.println("rows:"+rows); }

 

3 然后直接运行testClientInsert 即可成功

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 

posted @ 2020-05-07 11:19  AnyYoung  阅读(1029)  评论(0编辑  收藏  举报