简体   繁体   English

SpringBootTest 在使用 Docker 构建 Jenkins 期间由于 UnsatisfiedDependencyException 而失败

[英]SpringBootTest fails due to UnsatisfiedDependencyException during Jenkins build with Docker

First of all, it might look duplicate to other post on stackoverflow, and I tried couple of solutions.首先,它可能看起来与 stackoverflow 上的其他帖子重复,我尝试了几个解决方案。 But, none solving my issue.但是,没有人能解决我的问题。

Issue:问题:

Springboot test getting failed due to UnsatisfiedDependencyException during jenkins build.由于在 jenkins 构建期间出现 UnsatisfiedDependencyException,Springboot 测试失败。 But, interestingly, on local system I don't see same exception and I'm able to build same codebase without any error/exception.但是,有趣的是,在本地系统上我没有看到相同的异常,并且我能够构建相同的代码库而没有任何错误/异常。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myAppServiceImpl' defined in file [/use/jenkins/path to class/MyAppServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 1; org.springframework.beans.factory.UnsatisfiedDependencyException:在文件 [/use/jenkins/path to class/MyAppServiceImpl.class] 中定义的名称为“myAppServiceImpl”的 bean 创建时出错:通过构造函数参数 1 表示的依赖关系不满足;

I'm getting this issue only during jenkins build whereas on my local system.我仅在 jenkins 构建期间遇到此问题,而在我的本地系统上。 I'm able to build the same codebase without any issue.我能够毫无问题地构建相同的代码库。 So, I'm not sure what should I make change to make it work on platform.所以,我不确定我应该做些什么改变才能让它在平台上工作。

Due to restriction, I won't be able to share exact code, but I'm sharing similar to it.由于限制,我将无法分享确切的代码,但我分享的是类似的。

package com.tg.app.main.engine

@SpringBootApplication
@EnableConfigurationProperties(MyApplicationProperties.class)
@ComponentScan("com.tg.app")
class MyApplication{
// main method
}

Test测试

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT, classes=MyApplication.class)
@AutoConfigurationMockMvc
@AutoConfigureWebTestClient(timeout="100000")
class MyApplicationTest{

 @Autowire
 private MyAppService myAppService;

//codes

}

Service服务

   package com.tg.app.main.engine.service
   public interface MyAppService{
   //method declaration
   }

   @Service
   public MyAppServiceImpl implements MyAppService{
   private UserService userService
   private String xyz;

      @Autowire
      public MyAppServiceImpl(String xyz, UserService userService){
       this.xyz=xyz;
       this.userService=userService;
      }
   }

I also tried adding MyAppService as below as suggested in some post.我还尝试按照某些帖子中的建议添加MyAppService ,如下所示。

@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT, classes={MyApplication.class, MyAppService.class})

Then, one of the post on stackoverflow ( Must use mockbean instead of autowired ) suggesting not to use @MockBean (Although, I tried it, which didn't helped in my case).然后,关于 stackoverflow 的一篇文章( 必须使用 mockbean 而不是 autowired )建议不要使用@MockBean (尽管我尝试过,但对我的情况没有帮助)。 So, I'm not sure what should I do to fix this issue.所以,我不确定我应该怎么做才能解决这个问题。 Need help.需要帮忙。 Since, this is only occurring on platform, so not possible to hit and trail there multiple times.因为,这只发生在平台上,所以不可能多次击中和跟踪那里。

Please let me know, if any other information is required.如果需要任何其他信息,请告诉我。

==Edit== ==编辑==

The same code was earlier working fine.相同的代码之前工作正常。 This time we have upgraded the SpringBoot version from 2.0 to 2.1 and removed couple of unnecessary libraries.这次我们将 SpringBoot 版本从2.0升级到2.1并删除了几个不必要的库。 As mentioned above, code is just to give you an idea about the structure of code.如上所述,代码只是为了让您了解代码的结构。 They aren't actual code.它们不是实际的代码。 Another thing to note here is, I'm able to successfully build same code on local machine and even bring up the service without any issue .这里要注意的另一件事是,我能够在本地机器上成功构建相同的代码,甚至可以毫无问题地启动服务 So, I'm very sure, nothing wrong with the code.所以,我很确定,代码没有问题。 It's something to do with platform where this code is getting built.这与构建此代码的平台有关。

I noted couple of differences between my local machine and platform.我注意到我的本地机器和平台之间的一些差异。 First, build triggered using Docker , which isn't the case when I'm doing in local (just executing maven command).首先,使用Docker触发构建,当我在本地执行时不是这种情况(只需执行 maven 命令)。 And other one is, Integration Testing running using OpenJDK ( maven 3.3.9-jdk-8 ) on platform whereas on my local I'm using Java Hotspot (Oracle jdk-8) .另一个是,在平台上使用OpenJDK ( maven 3.3.9-jdk-8 ) 运行集成测试,而在我的本地我使用Java Hotspot (Oracle jdk-8) Other than this, I didn't see any difference between local system and platform.除此之外,我没有看到本地系统和平台之间有任何区别。

Thanks.谢谢。

The problem is with your String xyz in MyAppServiceImpl , Spring isn't actually finding an autowire candidate, you could add to your test class:问题在于 MyAppServiceImpl 中的 String xyzMyAppServiceImpl实际上并没有找到自动装配候选者,您可以添加到您的测试 class 中:

@MockBean
private String xyz = "whatever you are trying to inject";

This way you will surpass your failed dependency injection这样您将超越失败的依赖注入

Looking at code that is not currently causing the issue is a wast of time.查看当前没有导致问题的代码是浪费时间。

If you can`t share the actual code due to confidentiality, then you should create a test project that gives you the same error and share here.如果由于机密性而无法共享实际代码,那么您应该创建一个测试项目,它会给出相同的错误并在此处共享。

That been said, I advise you to check for any profiles in maven that are active in your machine, but is disabled in the jenkins environment, or vice versa.话虽如此,我建议您检查 maven 中的任何配置文件,这些配置文件在您的机器中处于活动状态,但在 jenkins 环境中被禁用,反之亦然。

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

相关问题 Hygieia Jenkins-Build Collector 不以 UnsatisfiedDependencyException 开头 - Hygieia Jenkins-Build Collector not starting with UnsatisfiedDependencyException Spring引导应用程序本地构建传递,但由于测试而在Jenkins中失败 - Spring boot application local build passes, but fails in Jenkins due to Tests 由于配置,我的Android项目在Jenkins上构建失败 - Build fails on Jenkins for my Android project due to config 由于“无法生成DH密钥对”,Jenkins构建失败 - Jenkins build fails due to “Could not generate DH keypair” 在Jenkins构建过程中,maven-jarsigner-plugin失败,但在命令行上却没有 - maven-jarsigner-plugin fails during Jenkins build but not on command line 由于无法构建依赖关系的发行版本,因此在Jenkins中Maven发行版构建失败 - Maven release build fails in Jenkins due to failure to build a release version of dependency Jenkins Build因SVNException而失败 - Jenkins Build fails with SVNException Docker:由于未找到 JAXB-API,Jenkins-jdk11 无法连接到 Nexus - Docker: Jenkins-jdk11 fails to connect to Nexus due to JAXB-API has not been found 由于单元测试中捕获了异常,因此在三叶草分析期间,Gradle无法构建 - Gradle fails build during clover analysis due to a caught exception in a unit test 由于SVNAuthenticationException,Hudson构建失败 - Hudson build fails due to SVNAuthenticationException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM