ABAP和Java SpringBoot的單元測試
ABAP
在ABAP類裡,本地類(Local Class)裡用關鍵字FOR TESTING宣告過的方法,
在單元測試啟動後會自動被呼叫到。
Spring Boot
在Spring及Spring Boot “Convention over configuration”的設定思路里,放在路徑src/test/java下面以Tests.java結尾的Java類會被當成單元測試類處理。
對上述專案執行命令列mvn clean install後,報錯誤訊息:
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test at org.springframework.util.Assert.state(Assert.java:70) at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:202) at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:137) at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:409) at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:323) at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:277) at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:112) at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:82) at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:120) at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:143) at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104) at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33) at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283) at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128) at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203) at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
從呼叫棧能看出,Spring Boot單元測試框架試圖自動找到一個被施加單元測試的Java類,但是失敗了,所以報了異常。
由於我這個SpringBoot專案的入口是com.sap.smartService.SmartServiceApplication, 因此我需要在單元測試啟動類裡指定這個入口類:
@SpringBootTest(classes = com.sap.smartService.SmartServiceApplication.class)
加上之後maven build成功:
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2218629/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- ABAP和Java的單元測試Unit TestJava
- SpringBoot單元測試Spring Boot
- 測試 之Java單元測試、Android單元測試JavaAndroid
- SpringBoot中的response和request模擬單元測試Spring Boot
- SpringBoot lombok配置單元測試Spring BootLombok
- java中的單元測試Java
- SpringBoot與單元測試JUnit的結合Spring Boot
- springboot系列文章之使用單元測試Spring Boot
- SpringBoot 單元測試入門實踐Spring Boot
- springboot 多模組下的單元測試配置Spring Boot
- 單元測試:單元測試中的mockMock
- 如何單元測試Java的private方法Java
- 四種ABAP單元測試隔離(test isolation)技術
- Java單元測試神器之MockitoJavaMockito
- Java單元測試技巧之PowerMockJavaMock
- 1.13-java單元測試junitJava
- Java中的單元測試與整合測試最佳實踐Java
- 單元測試-【轉】論單元測試的重要性
- ABAP和Java單例模式的攻防Java單例模式
- java單元測試:unit testing best practicesJava
- iOS 單元測試和 UI 測試快速入門iOSUI
- .Net單元測試xUnit和整合測試指南(1)
- Java Junit單元測試(入門必看篇)Java
- Java單元測試之JUnit 5快速上手Java
- Python和單元測試那些事兒Python
- 基於SpringBoot實現單元測試的多種情境/方法(二)Spring Boot
- 單元測試,只是測試嗎?
- Springboot整合JUnit5優雅進行單元測試Spring Boot
- SpringBoot-16-之整合MyBatis-xml篇+單元測試Spring BootMyBatisXML
- 單元測試-一份如何寫好單元測試的參考
- 單元測試的規範
- Apache Camel的單元測試Apache
- python 單元測試Python
- iOS 單元測試iOS
- Flutter 單元測試Flutter
- 單元測試 Convey
- 單元測試真
- golang單元測試Golang