简体   繁体   中英

Maven Junit test case crashing with java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

I have been trying to implement a Junit test case for a service which returns what are the different type of resources as shown in the Tester code below:

public class Tester {

    MyInfoService myInfoService=null;

    @Before
    public void setUp() throws Exception{
        myInfoService = new MyInfoService();
        System.out.println(" @#$#@ myInfoService ="+myInfoService.getAllMyResourceTypes().size());
    }

    @Test
    public void testResTypeAll() {
        List<MyTypeInfoBean> resTypeBeanList = myInfoService.getAllMyResourceTypes();
        assertEquals("Testing size for res type...", 18, resTypeBeanList.size());
    }
}

Service class where the error occurs:

public class MyInfoService implements MyInfoServiceRemote {

           // some code here

            @Override
            public List<MyTypeInfoBean> getAllMyResourceTypes() {
            List<MyResourceTypeInfoDTO> resourceList = new ArrayList<>();
            List<MyTypeInfoBean> resourceListBean = new ArrayList<>();
            MyResourceTypeInfoDTO myResTypeInfoDTO = null;
            try {
            HashMap<Integer,MyConstantsUtilClass.MyResourceTypes> restypemap =      (HashMap<Integer,   MyResourceTypes>)MyConstantsUtilClass.MyResourceTypes.getRestypemap();
            Set<Map.Entry<Integer, MyConstantsUtilClass.MyResourceTypes>>   restypemapEntrySet = restypemap.entrySet();
            for (Entry<Integer, MyResourceTypes> entry : restypemapEntrySet) {
                myResTypeInfoDTO = new MyResourceTypeInfoDTO();
                // code to read the entry values and populate my DTO
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            // size gets printed, so everything is fine till now
            System.out.println("@#$#@ resourceList ="+resourceList.size());
        // The resource list is printed correctly above.The problem begins below
                Mapper mapper=null;
                if (resourceList != null) {
                    System.out.println("resourceList is not null object");
                    System.out.print("Original contents of al: ");
                    try {
                // this does not work and get a NoClassDefError
                mapper= DozerBeanMapperSingletonWrapper.getInstance();          } catch(Exception e) {
                        e.printStackTrace();
                }
                Iterator<MyResourceTypeInfoDTO> itr = resourceList.iterator();
                while (itr.hasNext()) {
                    MyResourceTypeInfoDTO element = (MyResourceTypeInfoDTO)                     itr.next();
                    if (mapper == null)
                    System.out.println("Mapper is  NULL");
                    MyTypeInfoBean beanElement = mapper.map(element, MyTypeInfoBean.class);
                    resourceListBean.add(beanElement);
                    }
            return resourceListBean;
            }
    // other methods here
    }

Below is the error log I get on running the Junit test case:

java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at org.dozer.DozerBeanMapper.<clinit>(DozerBeanMapper.java:58)
    at org.dozer.DozerBeanMapperSingletonWrapper.getInstance(DozerBeanMapperSingletonWrapper.java:43)
at com.inv.service.MyInfoService.getAllResourceTypes(MyInfoService.java:508)
at com.bel.tropo.tester.Tester.setUp(Tester.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

My pom.xml file has the dozer dependency correctly mentioned as :

<dependency>
            <groupId>net.sf.dozer</groupId>
            <artifactId>dozer</artifactId>
            <version>5.3.2</version>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
</dependency>

The dependencies required by dozer are already resolved through mvn dependency:resolve.

Should I downgrade to a lower version of dozer and check if it works?

These two ( How can I resolve java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory? and dozer with maven ) questions don't seem to give me a solution or am I missing something? Any help would be great.

Restate your dozer dependency as follows:

<dependency>
    <groupId>net.sf.dozer</groupId>
    <artifactId>dozer</artifactId>
    <version>5.3.2</version>
</dependency>

The dozer-5.3.2-pom declares SLF4J as a transitive dependency so it will be provided for you as soon as you remove the unwanted exclusions from your Dozer declaration.

<dependency>
<groupId>
net.sf.dozer
</groupId> 
<artifactId>
dozer
</artifactId>
<version>
5.5.1
</version>
</dependency>

Updating the dozer version and doing a maven compile/install solved it for me besides getting rid of exclusions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM