我一直在尝试为服务实现Junit测试用例,该服务返回如下所示的Tester代码中所示的不同类型的资源:
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>
推土机所需的依赖关系已通过mvndependency: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声明中删除了不需要的排除项,就会立即为您提供。