{"action":"opened","issue":{"url":"https://api.github.com/repos/i042416/JavaTwoPlusTwoEquals5/issues/172","repository_url":"https://api.github.com/repos/i042416/JavaTwoPlusTwoEquals5","labels_url":"https://api.github.com/repos/i042416/JavaTwoPlusTwoEquals5/issues/172/labels{/name}","comments_url":"https://api.github.com/repos/i042416/JavaTwoPlusTwoEquals5/issues/172/comments","events_url":"https://api.github.com/repos/i042416/JavaTwoPlusTwoEquals5/issues/172/events","html_url":"https://github.com/i042416/JavaTwoPlusTwoEquals5/issues/172","id":376758230,"node_id":"MDU6SXNzdWUzNzY3NTgyMzA=","number":172,"title":"ABAP和Java SpringBoot的单元测试","user":{"login":"i042416","id":5669954,"node_id":"MDQ6VXNlcjU2Njk5NTQ=","avatar_url":"https://avatars3.githubusercontent.com/u/5669954?v=4","gravatar_id":"","url":"https://api.github.com/users/i042416","html_url":"https://github.com/i042416","followers_url":"https://api.github.com/users/i042416/followers","following_url":"https://api.github.com/users/i042416/following{/other_user}","gists_url":"https://api.github.com/users/i042416/gists{/gist_id}","starred_url":"https://api.github.com/users/i042416/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/i042416/subscriptions","organizations_url":"https://api.github.com/users/i042416/orgs","repos_url":"https://api.github.com/users/i042416/repos","events_url":"https://api.github.com/users/i042416/events{/privacy}","received_events_url":"https://api.github.com/users/i042416/received_events","type":"User","site_admin":false},"labels":[{"id":757814796,"node_id":"MDU6TGFiZWw3NTc4MTQ3OTY=","url":"https://api.github.com/repos/i042416/JavaTwoPlusTwoEquals5/labels/Pure%20Java","name":"Pure Java","color":"c2e0c6","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-11-02T10:42:54Z","updated_at":"2018-11-02T10:42:54Z","closed_at":null,"author_association":"OWNER","body":"\r\n# ABAP\r\n\r\n在ABAP类里，本地类(Local Class)里用关键字FOR TESTING声明过的方法，\r\n![clipboard1](https://user-images.githubusercontent.com/5669954/47905043-10666300-dec1-11e8-9a17-61c412795dd7.png)\r\n\r\n在单元测试启动后会自动被调用到。\r\n\r\n# Spring Boot\r\n在Spring及Spring Boot “Convention over configuration”的设定思路里，放在路径src/test/java下面以Tests.java结尾的Java类会被当成单元测试类处理。\r\n![clipboard2](https://user-images.githubusercontent.com/5669954/47905044-10fef980-dec1-11e8-864d-a21c852037a9.png)\r\n\r\n对上述项目执行命令行mvn clean install后，报错误消息：\r\n\r\n```java\r\njava.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test\r\n        at org.springframework.util.Assert.state(Assert.java:70)\r\n        at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:202)\r\n        at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:137)\r\n        at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:409)\r\n        at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:323)\r\n        at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:277)\r\n        at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:112)\r\n        at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:82)\r\n        at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:120)\r\n        at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105)\r\n        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152)\r\n        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:143)\r\n        at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49)\r\n        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)\r\n        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)\r\n        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)\r\n        at java.lang.reflect.Constructor.newInstance(Constructor.java:422)\r\n        at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)\r\n        at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)\r\n        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)\r\n        at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)\r\n        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)\r\n        at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)\r\n        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)\r\n        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)\r\n        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)\r\n        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)\r\n        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)\r\n        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)\r\n        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)\r\n```\r\n从调用栈能看出，Spring Boot单元测试框架试图自动找到一个被施加单元测试的Java类，但是失败了，所以报了异常。\r\n![clipboard3](https://user-images.githubusercontent.com/5669954/47905046-10fef980-dec1-11e8-9a6d-878f8f35aad3.png)\r\n\r\n\r\n由于我这个SpringBoot项目的入口是com.sap.smartService.SmartServiceApplication, 因此我需要在单元测试启动类里指定这个入口类：\r\n@SpringBootTest(classes = com.sap.smartService.SmartServiceApplication.class)\r\n![clipboard4](https://user-images.githubusercontent.com/5669954/47905047-11979000-dec1-11e8-8f4d-2e2aac3ef6f0.png)\r\n\r\n加上之后maven build成功：\r\n![clipboard5](https://user-images.githubusercontent.com/5669954/47905048-11979000-dec1-11e8-8b9e-200bf6088b5c.png)\r\n\r\n\r\n"}}
{"actor":{"display_login":"i042416"}}