# ClassCastException in ESTestCase + SLF4J

**URL:** https://discuss.elastic.co/t/classcastexception-in-estestcase-slf4j/69040
**Category:** Elasticsearch
**Created:** [December 14, 2016, 3:37pm UTC](https://discuss.elastic.co/t/classcastexception-in-estestcase-slf4j/69040 "2016-12-14T15:37:11Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![iref](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iref/32/13882_2.png) [@iref](https://discuss.elastic.co/u/iref)
#### Post date: [December 14, 2016, 3:37pm UTC](https://discuss.elastic.co/t/classcastexception-in-estestcase-slf4j/69040/1 "2016-12-14T15:37:11Z")

</div>

Hi,

I tried to use ESSingleNodeTestCase (Elasticsearch v5.0.1) in my project, but tests throw following exception:

```
java.lang.ClassCastException: org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext
    at __randomizedtesting.SeedInfo.seed([281258191F6A65AD:4F71E07F941ECA0E]:0)
    at org.apache.logging.log4j.core.LoggerContext.getContext(LoggerContext.java:178)
    at org.apache.logging.log4j.core.config.Configurator.setLevel(Configurator.java:290)
    at org.elasticsearch.common.logging.Loggers.setLevel(Loggers.java:149)
    at org.elasticsearch.common.logging.Loggers.setLevel(Loggers.java:144)
    at org.elasticsearch.index.SearchSlowLog.setLevel(SearchSlowLog.java:114)
    at org.elasticsearch.index.SearchSlowLog.<init>(SearchSlowLog.java:109)
    at org.elasticsearch.index.IndexModule.<init>(IndexModule.java:114)
    at org.elasticsearch.indices.IndicesService.createIndexService(IndicesService.java:406)
    at org.elasticsearch.indices.IndicesService.createIndex(IndicesService.java:378)
    at org.elasticsearch.cluster.metadata.MetaDataIndexTemplateService.validateAndAddTemplate(MetaDataIndexTemplateService.java:210)
    at org.elasticsearch.cluster.metadata.MetaDataIndexTemplateService.access$200(MetaDataIndexTemplateService.java:60)
    at org.elasticsearch.cluster.metadata.MetaDataIndexTemplateService$2.execute(MetaDataIndexTemplateService.java:170)
    at org.elasticsearch.cluster.ClusterStateUpdateTask.execute(ClusterStateUpdateTask.java:45)
    at org.elasticsearch.cluster.service.ClusterService.runTasksForExecutor(ClusterService.java:555)
    at org.elasticsearch.cluster.service.ClusterService$UpdateTask.run(ClusterService.java:896)

```

From what I was able to diagnose, the problem is caused by using Elasticsearch testing framework together with SLF4J + Logback. ES node tries to setup its logging by accessing log4j2-core LoggerContext, which isn't available because we use SLF4J bridge. Is there a workaround?  
I've created minimal project that reproduces error and can be found here [https://github.com/iref/es-slf4j-test-error](https://github.com/iref/es-slf4j-test-error)

Thank you for your help.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [December 14, 2016, 4:32pm UTC](https://discuss.elastic.co/t/classcastexception-in-estestcase-slf4j/69040/2 "2016-12-14T16:32:04Z")

</div>

You can not embed elasticsearch like in tests without using the exact libs elasticsearch requires.  
So SLF4J + Logback is not supported here.

You'd better run an external elasticsearch cluster for your tests.  
I described a cookbook here for Maven: [http://david.pilato.fr/blog/2016/10/18/elasticsearch-real-integration-tests-updated-for-ga/](http://david.pilato.fr/blog/2016/10/18/elasticsearch-real-integration-tests-updated-for-ga/)

HTH

---

<div class="post-metadata">

### Author: ![iref](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iref/32/13882_2.png) [@iref](https://discuss.elastic.co/u/iref)
#### Post date: [December 14, 2016, 5:09pm UTC](https://discuss.elastic.co/t/classcastexception-in-estestcase-slf4j/69040/3 "2016-12-14T17:09:22Z")

</div>

I was afraid that is the case. Thank you for link, I'll tak a look.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [January 11, 2017, 5:09pm UTC](https://discuss.elastic.co/t/classcastexception-in-estestcase-slf4j/69040/4 "2017-01-11T17:09:38Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
