[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 即可成功

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM