本教程后面有一个基于Spring Boot的独立(非Web)应用程序: https://www.mkyong.com/spring-boot/spring-boot-jdbc-mysql-hikaricp-example/
我跳过hikaricp部分直接使用mysql。 该应用程序工作正常,能够查询,调用RESTful Web服务......但是,我无法进行junit测试工作,不断收到此错误:
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:423)
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.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:36)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:275)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:149)
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)
相关的junit测试代码:
`
@RunWith(SpringRunner.class)
@RestClientTest(ArrivalRestClient.class)
public class ArrivalRestClientTest {
private static final String MOCK_URL = "http://baseURI/arrival";
@Autowired
private ArrivalRestClient clientUnderTest;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private MockRestServiceServer mockServer;
@Autowired
private RestTemplate restTemplate;
@Before
public void setUp() throws Exception {
mockServer = MockRestServiceServer.createServer(restTemplate);
clientUnderTest = new ArrivalRestClient(MOCK_URL);
ReflectionTestUtils.setField(clientUnderTest, "restTemplate", restTemplate);
}
@Test
public void testGetArrivals() throws JsonProcessingException {
Arrival arrival = new Arrival("JohnTest1", "GLBProduct", 0L);
String arrivalString = objectMapper.writeValueAsString(arrival);
this.mockServer.expect(requestTo("/arrival?source=source1&entity=entity1×tamp=1000")).andRespond(withSuccess(arrivalString, MediaType.APPLICATION_JSON));
Arrivals actual = clientUnderTest.getArrivals("JohnTest1", "GLBProduct", 0);
System.out.println("getExpectedtime: " + actual.getNextRunTime());
System.out.println("getPartitions: " + actual.getPartitions());
assertEquals((long)2000, actual.getNextRunTime());
assertEquals(new Long[]{ (long)2,(long)3}, actual.getPartitions());
}
}`
即使空单元测试失败也是如此。相信它与Spring Boot独立应用程序(非Web)有关。我无法找到关于如何在线装配的提示,感谢您的帮助!
编辑---还添加我的主类代码:
@SpringBootApplication
public class DepManagerMain implements CommandLineRunner {
final static Logger logger = Logger.getLogger(DepManagerMain.class);
@Autowired
DataSource dataSource;
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(DepManagerMain.class);
app.run(args);
}
@Override
public void run(String... args) throws Exception {
logger.info("DependencyManager starting");
logger.info("DATASOURCE = " + dataSource);
...
}
}
答案 0 :(得分:0)
据我所知,RestTemplate
注释只为您提供有限数量的bean。
我认为@SpringBootConfiguration
不是那些bean中的一个,所以我认为此时自动装配失败,因此它试图从主@RestClientTest
加载它。
RestTemplateBuilder
注释确实为您提供了RestTemplate
,但您可以自动将其加入并构建 <script src="//app-sjst.marketo.com/js/forms2/js/forms2.js"></script>
<form id="mktoForm_1057"></form>
<script>
MktoForms2.loadForm("//app-sjst.marketo.com", "785-UHP-775", 1057, function(form){
//Add an onSuccess handler
form.onSuccess(function(values, followUpUrl){
//get the form's jQuery element and hide it
form.getFormElem().hide();
//return false to prevent the submission handler from taking the lead to the follow up url.
return false;
});
});
</script>
。