使用 spring 启动的 Proguard - 集成测试

Proguard with spring boot - integration tests

我正在使用 ProGuard 进行 spring-boot 应用混淆。它工作正常,生成的 jar 已构建并且 运行 很好,没有任何问题。

为了实现这一点,我进行了一些调整:

但是,集成测试 (@SpringBootTest) 因错误而中断(它们 运行 在没有混淆器的情况下构建时很好):

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

包含主要条目后 class (@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: {}

以此类推

除了创建新的配置文件以执行测试而不混淆之外,还有什么方法可以让它工作吗?

preservation of Autowired, Qualifier, Bean, Value annotation members,

你做对了,你需要保留所有属性

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

然而这不是你的问题的原因,Spring 自动装配 byType 并且根据我的经验 post 混淆这样的问题来自没有找到类型的 bean。

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

@Component("yourComponentName")

而不是

@Component

bean和Service等注解相同

然后在任何地方使用 @Qualifier("nameofbeanorService")

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

谢谢

提取 BeanNameGenerator 以分离 Bean 并将其注册到 @SpringBootApplication 的 @ComponentScan 中,而不是在 main 方法中将其内联到 SpringApplicationBuilder 中使事情正常进行。