java - Maven Junit 测试用例因 java.lang.NoClassDefFoundError : org/slf4j/LoggerFactory 崩溃

标签 java maven junit dozer

我一直在尝试为服务实现 Junit 测试用例,该测试返回不同类型的资源,如下面的测试器代码所示:

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());
    }
}

发生错误的服务类:

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
    }

下面是我运行 Junit 测试用例时得到的错误日志:

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)

我的 pom.xml 文件具有正确提及的推土机依赖项:

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

dozer所需的依赖已经通过mvn dependency:resolve解决。

我应该降级到较低版本的推土机并检查它是否有效?

这两个( How can I resolve java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory?dozer with maven )问题似乎没有给我解决方案,还是我遗漏了什么? 任何帮助都会很棒。

最佳答案

重申您的推土机依赖性,如下所示:

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

dozer-5.3.2-pom将 SLF4J 声明为传递依赖项,因此一旦您从 Dozer 声明中删除不需要的排除项,就会立即为您提供它。

关于java - Maven Junit 测试用例因 java.lang.NoClassDefFoundError : org/slf4j/LoggerFactory 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47526527/

相关文章:

java - Tomcat 服务器和 JSP Web 应用程序的问题

java - Jersey Web 应用程序未部署在 tomcat 上

java - Maven:包 net.jcip.annotations 不存在

android - 如何在 android kotlin 中为 switch 条件编写测试用例

java - Spring Boot 在关闭时关闭 hibernate session - 在 @Async 方法完成之前

用于匹配 #<string>vs<string> 的 Java 正则表达式

java - 未找到 Maven 依赖项 IntelliJ Ultimate 2020

java - 测试调用 native 方法的代码

java - 我是否需要安装 Apache Spark 和/或 Scala 才能运行 jUnit?

java - 如何实现 `range()`