为了简化服务代码-我为SpringBoot应用程序创建了一个Super类,该类将由各种Spring Apps继承,从而避免了样板代码。
//No Annotations here
public class BootApplication extends SpringBootServletInitalizer{
public static final void startBootApp(String[] args,String[][]
customArgs){
customArgs contains Name-Value Pairs for any customization
to spring.config.name or spring.config.location etcetra
//Optional check on Custom Args
ArrayUtils.toMap(customArgs).forEach((k,v) -> Set the System
Property
});
}
@Bean
public WebserverFactoryCustomizer<TomcatServletWebServerFactory>
customizer(){
return container -> {
container.setPort(BootApplication.port);
}
}
private static void getMetaData(Class<?> application){
//Get Annotation Value of CustomSpringBootMetaData
x = application.getAnnotation(CustomSpringBootMetaData.class);
BootApplication.port=x.port();
BootApplication.configName=x.configName();
}
}//Class Ends
************************************************
@Target(Element.Type)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface CustomSpringBootMetaData {
int port() default 8080;
String configName() default "application";
}
**********************************************************
My Application Class
@CustomSpringBootMetaData (port=8080,configName=myApp)
@SpringBoootApplication(scanBasePackages="cxxxx"
MySpringBootApplication extends BootApplication {
//main method
BootApplication.startBootApp(args,null);//Assume Optional check is there in BootApplication
}
**********************************************************
My Test Class
@RunWith(SpringRunner.class)
@SpringBootTest(classes={MySpringBootApplication.class}){
@Test public void test() {..... };
}
问题 -运行Junit测试时-Junit不采用执行路径,即MyBootApplication-> main-> BootApplication。 startBootApp。运行测试时,它直接调用定制程序。
但是,当我使用java -jar xxx.jar或仅通过maven:spring-boot run启动应用程序时,将采用执行路径(MyBootApplication-> BootApplication.startBootApp)
我的测试配置中缺少什么或设计缺陷是什么?