gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:57:00 27 4
gpt4 key购买 nike

我一直在尝试为服务实现 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com