SpringBoot項目的測試類

 

 

1.java

package soundsystem; import static org.junit.Assert.*; 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; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes=CDPlayerConfig.class) public class CDPlayerTest { @Autowired private CompactDisc cd; @Test public void cdShouldNotBeNull() { assertNotNull(cd); } }

CDPlayerTest使用了Spring的SpringJUnit4ClassRunner,以便在測試開始的時候自動建立Spring的應用上下文。spring

註解@ContextConfiguration會告訴它須要在CDPlayerConfig中加載配置。測試

由於CDPlayerConfig類中包含了@ComponentScan,所以最終的應用上下文中應該包含CompactDiscbean。spa

 

2.code

package com.baizhi.cmfz; import com.baizhi.cmfz.dao.BannerDao; import com.baizhi.cmfz.entiy.Banner; import com.baizhi.cmfz.service.BannerService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.Date; import java.util.List; import java.util.Map; @RunWith(SpringRunner.class) @SpringBootTest public class BannerTest { @Autowired private BannerDao dao; @Autowired private BannerService service; @Test public void test1(){ List<Banner> list = dao.selectAllBanner(1,10,null); for (Banner banner : list) { System.out.println(banner); } } }

其中類SpringRunner繼承了了SpringJUnit4ClassRunnerblog

相關文章
相關標籤/搜索