3

I want to perform unit testing in Elasticsearch for that I am using Java-test-framework
I am using Elasticsearch-1.6.0 and referring to these link for help https://www.elastic.co/guide/en/elasticsearch/reference/1.6/using-elasticsearch-test-classes.html https://github.com/elastic/elasticsearch/blob/master/core/src/test/java/org/elasticsearch/action/search/SearchRequestBuilderTests.java

here is the code

class CampaignESTest extends ESTestCase {

  def getCLient():MockTransportClient={
     val settings = Settings.builder()
                .put(Environment.PATH_HOME_SETTING.getKey(), Files.createTempDir().toString())
                .build();
     val client = new MockTransportClient(settings);  
     client
  }
 }

class CampaignTestSearch extends PlaySpec{
  val client=new CampaignESTest

val response = client.prepareSearch("dbtest")
      .setTypes(CAMPAIGN_COLLECTION_NAME)
      .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
      .addFields("uuid","campaignName","artworkID","activationDate","_source")
      .setQuery(query)
      .execute()
      .actionGet()
  }

I am getting this exception

  DeferredAbortedSuite:
[info] Exception encountered when attempting to run a suite with class name: org.scalatest.DeferredAbortedSuite *** ABORTED ***
[info]   java.lang.NoClassDefFoundError: org/apache/logging/log4j/Logger
[info]   at org.elasticsearch.common.logging.Loggers.getLogger(Loggers.java:101)
[info]   at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:92)
[info]   at org.elasticsearch.bootstrap.BootstrapForTesting.<clinit>(BootstrapForTesting.java:78)
[info]   at org.elasticsearch.test.ESTestCase.<clinit>(ESTestCase.java:138)
[info]   at testcontrollers.campaign.CampaignTestSerach.<init>(CampaignTestSerach.scala:40)
[info]   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[info]   at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
[info]   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[info]   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
[info]   at java.lang.Class.newInstance(Class.java:442)
[info]   ...
[info]   Cause: java.lang.ClassNotFoundException: org.apache.logging.log4j.Logger
[info]   at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
[info]   at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[info]   at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
[info]   at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[info]   at org.elasticsearch.common.logging.Loggers.getLogger(Loggers.java:101)
[info]   at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:92)
[info]   at org.elasticsearch.bootstrap.BootstrapForTesting.<clinit>(BootstrapForTesting.java:78)
[info]   at org.elasticsearch.test.ESTestCase.<clinit>(ESTestCase.java:138)
[info]   at testcontrollers.campaign.CampaignTestSerach.<init>(CampaignTestSerach.scala:40)
[info]   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[info]   ...
[error] Uncaught exception when running testcontrollers.campaign.CampaignTestSerach: java.lang.NoClassDefFoundError: org/apache/logging/log4j/Logger

getting an exception on this line

val client=new CampaignESTest

in class CampaignTestSearch

here are the dependencies in build.sbt file

   "org.slf4j" % "slf4j-api" % "1.6.4",
                "org.elasticsearch" % "elasticsearch" % "1.6.0",
                "org.elasticsearch.test" % "framework" % "5.0.0" % "test",
                "org.apache.lucene" % "lucene-test-framework" % "4.10.4" % "test",
                "com.carrotsearch.randomizedtesting" % "randomizedtesting-runner" % "1.6.0" % "test",
                "org.apache.lucene" % "lucene-codecs" % "4.10.4" % "test",
                "org.apache.logging.log4j" % "log4j-core" % "2.8.2" ,
"org.apache.lucene" % "lucene-expressions" % "4.10.4",

i suspect the cause of the exception maybe due to some versions of the dependencies can anyone please help with the correct library dependency version to make it to work or is there something else which i am missing

Edit removed "test" from "org.apache.logging.log4j" % "log4j-core" % "2.8.2" Update build.sbt

"org.slf4j" % "slf4j-api" % "1.6.4",
                "org.elasticsearch" % "elasticsearch" % "1.6.0",
                "org.elasticsearch.test" % "framework" % "5.0.0" % "test",
                "org.apache.lucene" % "lucene-test-framework" % "4.10.4" % "test",
                "com.carrotsearch.randomizedtesting" % "randomizedtesting-runner" % "1.6.0" % "test",
                "org.apache.lucene" % "lucene-codecs" % "4.10.4" % "test",
                "org.apache.logging.log4j" % "log4j-core" % "2.8.2",
                "org.apache.logging.log4j" % "log4j-slf4j-impl" % "2.8.2" ,
                "org.apache.lucene" % "lucene-expressions" % "4.10.4",
swaheed
  • 3,671
  • 10
  • 42
  • 103
  • why you have added log4j jar as `test` dependency ? – sunkuet02 Aug 19 '17 at 06:57
  • i see in your configuration you have both log4j2 and slf4j. what's you intention in regards to that? 1. direct all slf4j calls to log4j2 implementation / 2. direct all log4j2 calls to slf4j (and use logback or whatever) . This may affect answer. – albert_nil Aug 19 '17 at 07:15
  • i am using "org.slf4j" % "slf4j-api" % "1.6.4", for logging in my app and i have added "org.apache.logging.log4j" % "log4j-core" % "2.8.2" only for removing this exception but it did not solve the problem – swaheed Aug 19 '17 at 07:35

2 Answers2

9

to fix your issue, add following missing dependency:

  • org.apache.logging.log4j:log4j-api:2.8.2

aside, not directly related to issue, add following dependency so that your own calls to slf4j api get directed to log4j2 implementation also:

  • org.apache.logging.log4j:log4j-slf4j-impl:2.8.2

ensure no other log implementation is present on classpath.

note: the "extra" suggestion i made is based in that you want to redirect all your log calls to log4j, no matter if you are calling slf4j api or directly log4j. If not, please state which log implementatjon you would like, since then the OP listed depenedencies is also missing something.

albert_nil
  • 1,648
  • 7
  • 9
  • i am using slf4j for log statements – swaheed Aug 19 '17 at 08:35
  • after following your instruction i am getting this exception Exception encountered when attempting to run a suite with class name: org.scalatest.DeferredAbortedSuite *** ABORTED *** [info] java.lang.NoClassDefFoundError: org/apache/logging/log4j/Logger [info] at org.elasticsearch.common.logging.Loggers.getLogger(Loggers.java:101) [info] at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:92) [info] at org.elasticsearch.bootstrap.BootstrapForTesting.(BootstrapForTesting.java:78) – swaheed Aug 19 '17 at 09:10
  • regarding your first comment to answer: slf4j does not write any log statement; it's just an api that delegates to actual implementations. That's the reason i'm saying that my assumption is that for your slf4j calls you want to use log4j2 also. regarding second comment: please paste your updated dependencies configuration. – albert_nil Aug 19 '17 at 09:21
  • you are missing the first dependency i listed regarding log4j-api – albert_nil Aug 19 '17 at 09:35
  • @alber_nil can you please look into this issue as well it will be grate favour https://stackoverflow.com/questions/45770719/elasticsearch-found-jar-hell-in-test-classpath – swaheed Aug 19 '17 at 13:29
0

Caused By: Cause: java.lang.ClassNotFoundException: org.apache.logging.log4j.Logger error indicates that, JVM is not able to found org.apache.log4j.Logger class in your project or application CLASSPATH. may be the reason of error is missing log4j.jar file into you project or class path. so you need to use below dependency:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17
  • error explained by OP is about elasticsearch calling directly log4j api (which is already listed on dependencies). So issue might be on dependencies configuration, but not on adding slf4j bindings – albert_nil Aug 19 '17 at 07:21