简体   繁体   中英

java.lang.IllegalStateException: Failed to load ApplicationContext error java spring-boot

Okay i went through a lot of answers but my problem is still unsolved. I am trying to test my controller -

@Controller
@Produces(MediaType.APPLICATION_JSON)
@RequestMapping(value = "/verify/email")
public class EmailController {

@Autowired
private ByEmail strategy;

@Autowired
private ApplicationConfig config;

@ResponseBody
@RequestMapping(method = RequestMethod.POST, produces = 
MediaType.APPLICATION_JSON)
public Request newRequest(@RequestBody VRequest request) throws 
IOException {
    log.info(request.toString());
    System.out.println(request.toString());
    request.setType(VType.EMAIL);
    Optional<Request> response = strategy.processRequest(request);
    return response.orElseThrow(BadRequestException::new);
}

And my test is -

@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@WebAppConfiguration
@SpringBootTest
public class TestEmailController {

@Mock
private ByEmail strategy;

@InjectMocks
@Autowired
private EmailController controller;

@Autowired
private MockMvc mockMvc;

@Test
public void contexLoads() throws Exception {
    assertThat(controller).isNotNull();
}

When I tried to run the test it is giving following error -

java.lang.IllegalStateException: Failed to load ApplicationContext

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
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.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'application': Unsatisfied dependency expressed through field 'configs'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applicationConfig': Could not bind properties to ApplicationConfig (prefix=company.services.verification, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 2 errors
Field error in object 'company.services.verification' on field 'redirectUrl': rejected value [null]; codes [NotNull.company.services.verification.redirectUrl,NotNull.redirectUrl,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [company.services.verification.redirectUrl,redirectUrl]; arguments []; default message [redirectUrl]]; default message [may not be null]
Field error in object 'company.services.verification' on field 'countries': rejected value [null]; codes [NotNull.company.services.verification.countries,NotNull.countries,NotNull.java.util.List,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [company.services.verification.countries,countries]; arguments []; default message [countries]]; default message [may not be null]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    ... 24 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applicationConfig': Could not bind properties to ApplicationConfig (prefix=company.services.verification, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 2 errors
Field error in object 'company.services.verification' on field 'redirectUrl': rejected value [null]; codes [NotNull.company.services.verification.redirectUrl,NotNull.redirectUrl,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [company.services.verification.redirectUrl,redirectUrl]; arguments []; default message [redirectUrl]]; default message [may not be null]
Field error in object 'company.services.verification' on field 'countries': rejected value [null]; codes [NotNull.company.services.verification.countries,NotNull.countries,NotNull.java.util.List,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [company.services.verification.countries,countries]; arguments []; default message [countries]]; default message [may not be null]
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:334)
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:291)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    ... 42 more
Caused by: org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 2 errors
Field error in object 'company.services.verification' on field 'redirectUrl': rejected value [null]; codes [NotNull.company.services.verification.redirectUrl,NotNull.redirectUrl,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [company.services.verification.redirectUrl,redirectUrl]; arguments []; default message [redirectUrl]]; default message [may not be null]
Field error in object 'company.services.verification' on field 'countries': rejected value [null]; codes [NotNull.company.services.verification.countries,NotNull.countries,NotNull.java.util.List,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [company.services.verification.countries,countries]; arguments []; default message [countries]]; default message [may not be null]
    at org.springframework.boot.bind.PropertiesConfigurationFactory.checkForBindingErrors(PropertiesConfigurationFactory.java:359)
    at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:276)
    at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:240)
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:329)
    ... 55 more

this is my ApplicationTest class -

 @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ApplicationTests {

    @Test
    public void contextLoads() {
    }

}

Here is my ApplicationConfig class -

@Data
@Configuration
@EnableAutoConfiguration
@ConfigurationProperties(prefix = "company.services.verification")
public class ApplicationConfig {

@NotNull
String smstype;

@NotNull
String fromEmail;

@NotNull
String subjectEmail;

@NotNull
String subjectManual;

@NotNull
List<Integer> countries;

@NotNull
String redirectUrl;
}

And my application.yaml file where the abover properties are defined -

company:
    services:
      verification:
        smstype: Claims
        fromEmail: noreply@company.com
        subjectEmail: Email Verification
        subjectManual: Claim Initiated
        redirectUrl: http://www.google.com
        countries:
         - 1
         - 2
         - 3
         - 4

I went through a lot of answers but the error remains the same. I am using spring-boot 1.5.6. Any help please?

Try with this application.yaml, just one tab for all properties after verification section.

company:
    services:
      verification:
        smstype: Claims
        fromEmail: noreply@company.com
        subjectEmail: Email Verification
        subjectManual: Claim Initiated
        redirectUrl: http://www.google.com
        countries:
          - 1
          - 2
          - 3
          - 4

Option2: instead of .yaml use application.propterties create the application.propterties file in the resources folder and the content should be like this:

company.services.verification.smstype: Claims
company.services.verification.fromEmail: noreply@company.com
company.services.verification.subjectEmail: Email Verification
company.services.verification.subjectManual: Claim Initiated
company.services.verification.redirectUrl: http://www.google.com
company.services.verification.countries[0]: 1
company.services.verification.countries[1]: 2
company.services.verification.countries[2]: 3
company.services.verification.countries[3]: 4

Example of a Config Class

@Configuration
@ConfigurationProperties(prefix = "company.services.verification")
public class MyConfig {


    private String smstype;


    private String fromEmail;


    private String subjectEmail;


    private String subjectManual;


    private List<Integer> countries;


    private String redirectUrl;

    public String getSmstype() {
        return smstype;
    }

    public void setSmstype(String smstype) {
        this.smstype = smstype;
    }

    public String getFromEmail() {
        return fromEmail;
    }

    public void setFromEmail(String fromEmail) {
        this.fromEmail = fromEmail;
    }

    public String getSubjectEmail() {
        return subjectEmail;
    }

    public void setSubjectEmail(String subjectEmail) {
        this.subjectEmail = subjectEmail;
    }

    public String getSubjectManual() {
        return subjectManual;
    }

    public void setSubjectManual(String subjectManual) {
        this.subjectManual = subjectManual;
    }

    public List<Integer> getCountries() {
        return countries;
    }

    public void setCountries(List<Integer> countries) {
        this.countries = countries;
    }

    public String getRedirectUrl() {
        return redirectUrl;
    }

    public void setRedirectUrl(String redirectUrl) {
        this.redirectUrl = redirectUrl;
    }
}

First of all, please add the following dependency to you project and follow the Spring Doc instructions for " Generating your own meta-data using the annotation processor "

http://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/html/configuration-metadata.html#configuration-metadata-annotation-processor

Here is the maven dependency for generating custom spring meta-data configuration properties

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

Your applicationconfig class should also have getters and setters

@Configuration
@EnableAutoConfiguration
@ConfigurationProperties(prefix = "company.services.verification")
public class ApplicationConfig {

    @NotNull
    String smstype;

    @NotNull
    String fromEmail;

    @NotNull
    String subjectEmail;

    @NotNull
    String subjectManual;

    @NotNull
    List<Integer> countries;

    @NotNull
    String redirectUrl;

    public String getSmstype() {
        return smstype;
    }

    public void setSmstype(String smstype) {
        this.smstype = smstype;
    }

    public String getFromEmail() {
        return fromEmail;
    }

    public void setFromEmail(String fromEmail) {
        this.fromEmail = fromEmail;
    }

    public String getSubjectEmail() {
        return subjectEmail;
    }

    public void setSubjectEmail(String subjectEmail) {
        this.subjectEmail = subjectEmail;
    }

    public String getSubjectManual() {
        return subjectManual;
    }

    public void setSubjectManual(String subjectManual) {
        this.subjectManual = subjectManual;
    }

    public List<Integer> getCountries() {
        return countries;
    }

    public void setCountries(List<Integer> countries) {
        this.countries = countries;
    }

    public String getRedirectUrl() {
        return redirectUrl;
    }

    public void setRedirectUrl(String redirectUrl) {
        this.redirectUrl = redirectUrl;
    }
}

EmailController.class

@Controller
@Produces(MediaType.APPLICATION_JSON)
@RequestMapping(value = "/verify/email")
public class EmailController {

    @Autowired
    private ApplicationConfig config;

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
    public String newRequest(@RequestBody String email) throws
            IOException {

        return email;
    }
}

Since i dont have any idea about your byEmail class in your controller i removed it.

Application.yaml

company:
  services:
    verification:
      from-email: noreply@company.com
      redirect-url: http://www.google.com
      subject-email: Email Verification
      smstype: Claims
      subject-manual: Claim Initiated
      countries:
        - 1
        - 2
        - 3
        - 4

And here is the most important part, you should add applicationcontext to your test classes also and make necessary mocking operations before any test.

EmailControllerTests.java

@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@WebAppConfiguration
@SpringBootTest
public class EmailControllerTests {
    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext applicationContext;

    @Mock
    private ApplicationConfig applicationConfig;

    @Before
    public void init() throws Exception {
        MockitoAnnotations.initMocks(this);

        // @formatter:off
        this.mockMvc = webAppContextSetup(this.applicationContext)
                .alwaysDo(print())
                .build();
        // @formatter:on

        EmailController emailController = this.applicationContext.getBean(EmailController.class);

        // REPLACE YOUR SERVICES WITH the MOCKED ONES HERE
        //ReflectionTestUtils.setField(emailController, "byEmail", this.byEmail);
        ReflectionTestUtils.setField(emailController, "config", this.applicationConfig);
    }

    @Test
    public void unauthenticatedUsersCannotAddProperty() throws Exception {
        // @formatter:off
        MvcResult result = this.mockMvc.perform(post("/verify/email").contentType(MediaType.APPLICATION_JSON).content("{ahmet@ahmet.com}"))
                .andDo(print())
            .andExpect(status().isOk())
            .andReturn();
        // @formatter:on

        Assert.assertNotNull(result);
    }
}

And here is the result, everything is working as it should be...

在此处输入图片说明

Hope this helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM