junit測試(springboot/springMVC)

springboot:

1、在需要測試的類輸入快捷鍵:ctrl+shift+T

2、在生成的測試類加入註解:其中SyncManageApplication是對應的啓動類

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = SyncManageApplication.class)

3、引入依賴加入對應的Assert

 

springMVC:

1、創建測試基類:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:test-service.xml")
public abstract class BaseRollbackTest extends AbstractTransactionalJUnit4SpringContextTests {
}

2、在test目錄的resource中配置基類中的依賴location的xml文件: import resource 指定的是項目配置文件目錄

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
   
    <import resource="classpath*:META-INF/spring/*.xml"/> 
   
</beans>

3、創建測試類extends 基類即可(快捷鍵:ctrl+shift+T)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章