Need to create mock args for creation of an Environment object using app.run

Viewed 135

I am creating tests using JUnit. Need to mock args for creating an Environment variable.

String[] testStr = new String[]{
                "--spring.main.web-environment=false" //,
                //"--spring.autoconfigure.exclude=blahblahblah"  
                // Override any other environment properties according to your needs
        };


        //String[] testStr = new String[]{ 
        JHipsterConstants.SPRING_PROFILE_DEVELOPMENT,JHipsterConstants.SPRING_PROFILE_PRODUCTION };

        //Throwing error here
        app.main(testStr);

Method I am calling

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(TestApp.class);
        DefaultProfileUtil.addDefaultProfile(app);
        // line below fails. 
        Environment env = app.run(args).getEnvironment();
        logApplicationStartup(env);
    }

Fails on env = app.run(args).getEnvironment(). The test hesitates for several seconds and errors out. Any solutions will be appreciated. See error text below.

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'swaggerSpringfoxApiDocket', defined in class path resource [io/github/jhipster/config/apidoc/SwaggerConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [io/github/jhipster/config/apidoc/SwaggerAutoConfiguration.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true


org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'swaggerSpringfoxApiDocket' defined in class path resource [io/github/jhipster/config/apidoc/SwaggerConfiguration.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=io.github.jhipster.config.apidoc.SwaggerConfiguration; factoryMethodName=swaggerSpringfoxApiDocket; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [io/github/jhipster/config/apidoc/SwaggerConfiguration.class]] for bean 'swaggerSpringfoxApiDocket': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=io.github.jhipster.config.apidoc.SwaggerAutoConfiguration; factoryMethodName=swaggerSpringfoxApiDocket; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [io/github/jhipster/config/apidoc/SwaggerAutoConfiguration.class]] bound.

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:927)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:287)

0 Answers
Related