我一直在尝试为服务实现一个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依赖关系解析:resolve。
我应该降级到较低版本的推土机并检查它是否有效?
这两个(How can I resolve java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory?和dozer with maven)问题似乎没有给我一个解决方案,或者我错过了什么? 任何帮助都会很棒。
答案 0 :(得分:1)
重申您的推土机依赖关系,如下所示:
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.3.2</version>
</dependency>
dozer-5.3.2-pom将SLF4J声明为传递依赖关系,因此只要从Dozer声明中删除不需要的排除项,就会立即为您提供。