繁体   English   中英

带弹簧启动的Proguard-集成测试

[英]Proguard with spring boot - integration tests

我正在使用ProGuard进行春季启动应用程序混淆。 它工作正常,生成的jar得以构建并且运行良好,没有任何问题。

为此,我进行了一些调整:

  • 自定义beanNameGenerator

      public static void main(String[] args) { new SpringApplicationBuilder(MainApp.class) .beanNameGenerator((beanDefinition, beanDefinitionRegistry) -> beanDefinition.getBeanClassName()) .run(args); } 
  • 保留自动装配,限定符,Bean,值注释成员,
  • 保留主要方法
  • keepattributes开关,带有异常,内部类,签名,已弃用,SourceFile,LineNumberTable, Annotation和EnclosingMethod
  • dontshrink,dontoptimize,useuniqueclassmembernames,adaptclassstrings,dontusemixedcaseclassnames选项

但是,集成测试(@SpringBootTest)因错误而中断(在不使用proguard的情况下构建时,它们运行良好):

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test.

在包含主条目类(@SpringBootTest(classes = MainApp.class))之后:

2019-09-11 19:00:13,178 [main] ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@6b04acb2] to prepare te
st instance [com.xxx.xxx.it.ExternalIT@258274cb]
java.lang.IllegalStateException: Failed to load ApplicationContext
        at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) ~[spring-test-5.1.7.RELEASE.jar:5.1.7.RELEASE]
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.xxx.xxx.a.a.b' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

等等。

除了创建用于执行测试而不会混淆的新配置文件之外,还有其他任何方法可以使它工作?

保留自动装配,限定符,Bean,值注释成员,

您在这里做了正确的事,您需要保留所有属性

-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,LocalVariable*Table,*Annotation*,Synthetic,EnclosingMethod

但是,这不是您的问题的原因,Spring会自动装配byType,而根据我的经验,混淆后的问题是找不到类型的bean。

您需要调整代码,使用名称如下的注释

@Component( “yourComponentName”)

代替

@零件

与bean和Service以及其他注释相同

以及无论您在何处使用@Qualifier("nameofbeanorService")

并且由于保留了注释属性,因此您的应用程序将运行顺畅。

谢谢

提取BeanNameGenerator以分离Bean并在@SpringBootApplication的@ComponentScan中注册它,而不是在主要方法中的SpringApplicationBuilder中内联它,这可以使事情正常进行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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