Questions
- How to register the "refresh" scope on SpringBoot 1.4.x when using the
@RunWith(SpringRunner.classand@WebMvcTest? - Error message is
No Scope registered for scope name 'refresh'while loading a service - I'm using the annotations for the new version of the Tests with Spring MVC, including
@EnableConfigurationProperties - Error occurs when the Controller tries to call an
@Autowired Service
@RunWith(SpringRunner.class)
//All the beans loaded by the controller, transitively.
@WebMvcTest(...) // See below for details
@EnableConfigurationProperties
Stack Trace
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: No Scope registered for scope name 'refresh'
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:155)
at com.intuit.platform.config.publisher.github.GithubWebhookControllerAbstractTest.request(GithubWebhookControllerAbstractTest.java:48)
at com.intuit.platform.config.publisher.github.GithubWebhookControllerFullIntegrationTest.fullWebhookE2ETest(GithubWebhookControllerFullIntegrationTest.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalStateException: No Scope registered for scope name 'refresh'
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:337)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.getTarget(CglibAopProxy.java:687)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:637)
at com.intuit.platform.config.publisher.onboard.OnboardService$$EnhancerBySpringCGLIB$$32e5f6df.getOnboard(<generated>)
at com.intuit.platform.config.publisher.github.GithubWebhookController.handle(GithubWebhookController.java:130)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
... 48 more
Controller
@Autowired
private OnboardService onboardService;
@RequestMapping(value = "/publish", method = RequestMethod.POST)
public ResponseEntity<Set<MatrixFilesResponse>> handle(@RequestHeader("X-Hub-Signature") String signature,
@RequestHeader("X-GitHub-Event") String eventName, @RequestBody String eventPayload,
@RequestHeader("X-GitHub-Delivery") String delivery) {
...
... // Verify a couple of things with the full delivery object
...
// Parse what we need from the event
Event event = JsonUtil.toObject(eventPayload, Event.class);
// Load the related object from the service...
//>>>>>>>>>>>ERROR OCCURS IN THIS LINE BELOW <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Onboard registration = onboardService.getOnboard(event.getRepository().getName());
Test Class
@RunWith(SpringRunner.class)
//All the beans loaded by the controller, transitively.
@WebMvcTest({GithubWebhookController.class, ServiceBuildProperties.class, OnboardService.class, MatrixFileService.class,
S3SyncService.class, S3SyncProperties.class, GithubCloneService.class})
@EnableConfigurationProperties
public class GithubWebhookControllerFullIntegrationTest extends GithubWebhookControllerAbstractTest {
private static final String FIXTURE_FILE = "fixtures/github/webhook-payload-master-formatted.json";
@Autowired
private MockMvc mvc;
/**
* The controller request.
*/
protected static final String PATH = "/v1/publish";
@Test
public void fullWebhookE2ETest() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.put("X-Hub-Signature", Arrays.asList("sha1=887cdc40d91a7ea99b6bd7a07b0d7e1a8eb0fc42"));
headers.put("X-GitHub-Event", Arrays.asList("push"));
headers.put("X-GitHub-Delivery", Arrays.asList("797af380-69a5-11e6-857c-9358cc671df3-test-case"));
MatrixFilesResponse matrixFileResponse = this.request(FIXTURE_FILE, "", headers);
// Verify that the label was returned properly.
assertThat(matrixFileResponse.getLabel()).isEmpty();
assertThat(matrixFileResponse.getS3Bucket()).isEqualTo(MatrixFilesResponse.DEFAULT_BUCKET_NAME);
assertThat(matrixFileResponse.getOutputTypes()).containsExactly("yml", "json");
}
protected MatrixFilesResponse request(String inputFileName, String params, HttpHeaders httpHeaders) throws Exception {
String input = loadWebhookPayload(inputFileName);
assertThat(input).isNotNull();
// HTTP POST to the given path and provided parameters.
String responseJson = new String(mvc.perform(post(PATH + params)
.content(input)
.contentType(MediaType.APPLICATION_JSON)
.headers(httpHeaders)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsByteArray());
assertThat(responseJson).isNotNull();
return JsonUtil.toObject(responseJson, MatrixFilesResponse.class);
}
}
Comment From: wilkinsona
As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. In the future, please ask this sort of question on Stack Overflow, particularly as it concerns two projects: Spring Boot and Spring Cloud.
Spring Cloud uses RefreshAutoConfiguration to add the refresh scope to your application. By default, this auto-configuration isn't included in the auto-configuration that's enabled by @WebMvcTest. You can enable extra auto-configuration by adding @ImportAutoConfiguration(RefreshAutoConfiguration.class) to your tests.
Comment From: marcellodesales
@wilkinsona Thank you and I apologize for opening this.
I got away using the @SpringBootTest annotation.
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class GithubWebhookControllerFullIntegrationTest {
}
I will try to use this in the future.
Comment From: cesargithub5
I had this problem and I solved it using the recommendation that wilkinson said.
@RunWith(SpringJUnit4ClassRunner.class)
@ImportAutoConfiguration(RefreshAutoConfiguration.class)
@ContextConfiguration(classes={myConfigClass.class})
public class ApplicationUT {
}
Comment From: hqthai05
Please use property "spring.datasource.type =
Comment From: vimal3271
I am using spring 3.3.1. The above solution is not working. Tried this
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT
Any workaround ?