UnknownHostException for embedded Mongo DB tests in Jenkins build
I managed to reproduce the same error locally and, if you haven't solved it yet, this is my take.
Remove all MongoDB custom configurations/properties, then add the following class to your project:
@Configurationpublic class MongoTestConfig { @Bean MongoProperties properties() { MongoProperties mongoProperties = new MongoProperties(); mongoProperties.setPort(33333); mongoProperties.setHost("localhost"); return mongoProperties; }}
Make sure you tag your test class (UserRepositoryTest) with these three annotations:
@RunWith(SpringRunner.class)@DataMongoTest@Import(MongoTestConfig.class)
@DataMongoTest ignores all other bean definitions so we force the test to include the configuration that we just created using the explicit @Import.
Now the test will be forced to run on localhost. Hope it's going to work for you as it did for me! 😁
If dev096.dev.cloud.******.eu
is a host used only in production, then your production server needs to know that host; not your local PC, nor Jenkins.
Ideally, you'd run your Jenkins tests using a 'jenkins' Spring profile, and then define the localhost
in application-jenkins.properties.
Unfortunately, none of your solutions worked for me, but I found out later that dev096.dev.cloud.******.eu
is the Jenkins instance itself.
The fix was to add an entry in /etc/hosts/
such that the server's IP to be also recognized as dev096.dev.cloud.******.eu