Search code examples
togglz

Provider org.togglz.slf4j.Slf4jLogProvider not a subtype


I have a library, built with Maven, that uses Spring 4.0.3.RELEASE and Togglz 2.2.0.Final. I'm trying to write a JUnit 4.11 test of my Spring class and running into the following error on the first test that gets executed:

testCreateItem_throwsItemServiceBusinessException(impl.ItemServiceImplTest)  Time elapsed: 1.771 sec  <<< ERROR!
java.util.ServiceConfigurationError: org.togglz.core.spi.LogProvider: 
Provider org.togglz.slf4j.Slf4jLogProvider not a subtype

Here is the relevant java test snippet:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
@PrepareForTest({ ItemServiceImpl.class })
public class ItemServiceImplTest {
   @Rule
   public TogglzRule togglzRule = TogglzRule.allDisabled(Features.class);

   @Rule
   public PowerMockRule powerMockRule = new PowerMockRule();

   @Test(expected = ItemServiceBusinessException.class)
   public void testCreateItem_throwsItemServiceBusinessException() throws Exception {
      PowerMockito.doReturn(mockMetricsData).when(serviceUnderTest, START_METRICS_METHOD_NAME, any(MetricsOperationName.class), any(RequestContext.class));

      when(mockDao.createItem(any(Item.class), any(RequestContext.class))).thenThrow(dataBusinessException);

      serviceUnderTest.createItem(item, context);

      verify(mockItemServiceValidator).validate(any(Item.class), any(RequestContext.class));

      PowerMockito.verifyPrivate(serviceUnderTest).invoke(START_METRICS_METHOD_NAME, any(MetricsOperationName.class), any(RequestContext.class));

      verify(mockDao).createItem(any(Item.class), any(RequestContext.class));
   }
}

Subsequent test calls get the following error:

java.lang.NoClassDefFoundError: Could not initialize class org.togglz.junit.TogglzRule

Here are some relevant dependencies I have:

 org.mockito:mockito-all=org.mockito:mockito-all:jar:1.9.5:compile,
 org.powermock:powermock-module-junit4=org.powermock:powermock-module-junit4:jar:1.5.6:test,org.powermock:powermock-module-junit4-common=org.powermock:powermock-module-junit4-common:jar:1.5.6:test,
 org.powermock:powermock-reflect=org.powermock:powermock-reflect:jar:1.5.6:test,
 org.powermock:powermock-api-mockito=org.powermock:powermock-api-mockito:jar:1.5.6:test,
 org.powermock:powermock-api-support=org.powermock:powermock-api-support:jar:1.5.6:test,
 org.powermock:powermock-module-junit4-rule=org.powermock:powermock-module-junit4-rule:jar:1.5.6:test,
 org.powermock:powermock-classloading-base=org.powermock:powermock-classloading-base:jar:1.5.6:test,
 org.powermock:powermock-core=org.powermock:powermock-core:jar:1.5.6:test,
 org.powermock:powermock-classloading-xstream=org.powermock:powermock-classloading-xstream:jar:1.5.6:test,
 org.togglz:togglz-core=org.togglz:togglz-core:jar:2.2.0.Final:compile,
 org.togglz:togglz-slf4j=org.togglz:togglz-slf4j:jar:2.2.0.Final:compile,
 org.togglz:togglz-spring-core=org.togglz:togglz-spring-core:jar:2.2.0.Final:compile,
 org.togglz:togglz-testing=org.togglz:togglz-testing:jar:2.2.0.Final:test,
 org.togglz:togglz-junit=org.togglz:togglz-junit:jar:2.2.0.Final:test

And I have provided a LogProvider (org.togglz.slf4j.Slf4jLogProvider) via SPI, located at META-INF/serivces/org.togglz.core.spi.LogProvider

This error is baffling as Slf4jLogProvider should be assignable from LogProvider. Sorry for the verbosity, but I wanted to try and show a complete picture. The code in class "under test" is making a call to see if a single feature is enabled inside the create method.


Solution

  • First of all: You don't need to configure the log provider in your application. Including togglz-slf4j on your application path is sufficient because this jar contains the corresponding SPI file.

    Could you please check if there are multiple conflicting versions of the Togglz JAR files on your classpath? For example using togglz-core-2.2.0.Final.jar together with togglz-slf4j-2.1.0.Final.jar could result in an error like this.

    This can happen if you update Togglz and your IDE didn't remove the old archives. Running a clean build and/or selecting "Update Maven Configuration" on Eclipse will fix this problem.