2012-01-08 88 views
1

嗨,我想使用junit,它不工作。與Eclipse的junit測試

這是我的代碼。

package safe; 


import java.lang.reflect.*; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.List; 
import safe.SafetyException; 

public class SafetyInspector { 

public static boolean isSafe(Class<?> clazz) throws SafetyException{ 


    if (clazz.equals(Object.class)) return true; 
    if (clazz == null) { 
     throw new SafetyException(); 
    } 

    Field fields[] = clazz.getDeclaredFields(); 
    for(Field f: fields){ 
     f.setAccessible(true); 
     int mod = f.getModifiers(); 
     if (Modifier.isFinal(mod)){ 
      continue; 
     } 
     else if (Modifier.isPrivate(mod)){ 
      Class<?> typeArray[] = new Class<?>[1] ; 
      typeArray[0] = f.getType(); 
      try { 
       Method mSet = clazz.getMethod("set" + f.getName().substring(0, 0).toUpperCase() + f.getName().substring(1),typeArray); 
       int modMet = mSet.getModifiers(); 
       if(!Modifier.isPublic(modMet)) return false; 
       if(!mSet.getReturnType().equals(void.class)) return false; 
      } 
      catch (SecurityException e) { 

       throw new SafetyException(); 

      } 
      catch (NoSuchMethodException e) { 

       return false; 
      } 

      try { 
       Class<?> typeArray2[] = new Class<?>[1] ; 
       Method mGet = clazz.getMethod("get" + f.getName().substring(0, 0).toUpperCase() + f.getName().substring(1),typeArray2); 
       int modMet2 = mGet.getModifiers(); 
       if(!Modifier.isPublic(modMet2)) return false; 

       if(!mGet.getReturnType().equals(f.getType())) return false; 
      } 
      catch (SecurityException e) { 

       throw new SafetyException() ; 
      } 
      catch (NoSuchMethodException e) { 

       return false; 
      } 





     } 



    } 


    return isSafe(clazz.getSuperclass()); 

} 

public static void sort(List<Class<?>> classes) throws SafetyException{ 

    for (int i = 1; i < classes.size(); i++) { 
     for (int j = 0; j < classes.size() - i; j++) { 
      if (compare(classes.get(j), classes.get(j + 1)) > 0) { 
       swap(classes, j); 
      } 
     } 
    }  

} 

public static void reset(Object object) throws SafetyException{ 

     Class c = object.getClass(); 
     Field fields[] = c.getDeclaredFields(); 
     for(Field f :fields){ 
      if (!isSafe(f.getClass())) 
      { 
       f.setAccessible(true); 
       try{ 
        if(!f.getClass().isPrimitive()){ 

        } 
        else if(f.getClass().equals(boolean.class)){ 
         f.setBoolean(object, false); 

        } 
        else{ 
         f.set(object, 0); 
        } 
       } 

       catch(Exception e) 
       { 
        throw new SafetyException(); 
       } 


      } 
     } 



} 




private static int compare(Class<?> o1, Class<?> o2) throws SafetyException { 

     Field[] fields1 = o1.getDeclaredFields(); 
     int count1 = 0; 
     for (Field f : fields1){ 
      if (isSafe(f.getClass())) count1++; 

     } 
     Field[] fields2 = o2.getDeclaredFields(); 
     int count2 = 0; 
     for (Field f : fields2){ 
      if (isSafe(f.getClass())) count2++; 

     } 

     if (count1 == count2) 
      return o1.getName().compareTo(o2.getName()); 

     else return count1- count2; 

    } 

private static void swap(List<Class<?>> list, int j) { 
    Class<?> temp = list.get(j); 
    list.set(j, list.get(j+1)); 
    list.set(j + 1, temp); 
} 



}; 

這裏是他們給我的代碼junit測試。

package test; 

import static org.junit.Assert.assertEquals; 


import java.util.Collections; 
import java.util.LinkedList; 
import java.util.List; 

import org.junit.Test; 

import safe.SafetyException; 
import safe.SafetyInspector; 


public class SafetyInspectorTest { 
@Test 
public void testIsSafeEmployee() throws SafetyException { 
    assertEquals(false, SafetyInspector.isSafe(Employee.class)); 
} 

@Test 
public void testResetEmployee() throws SafetyException { 
    Employee e = new Employee(123,3000); 
    SafetyInspector.reset(e); 
    assertEquals(0, e.id); 
    assertEquals(3000, e.getSalary()); 
} 

@Test 
public void testSort() throws SafetyException { 
    List<Class<?>> sortedClasses = new LinkedList<Class<?>>(); 
    sortedClasses.add(Employee.class); 
    List<Class<?>> classes = new LinkedList<Class<?>>(sortedClasses); 
    Collections.shuffle(classes); 
    SafetyInspector.sort(classes); 
    assertEquals(sortedClasses, classes); 
} 
} 

當我運行safetyInspectorTest作爲junitTESTCLASS我得到一個初始化錯誤。如果它有幫助,我將使用eclipse,並將Junit作爲項目的庫。

+0

聽起來像一個類路徑問題。 – 2012-01-08 13:52:45

+0

我將junit.rar添加到類路徑 – user1088557 2012-01-08 14:10:40

+0

那麼問題是什麼呢? – 2012-01-08 14:14:05

回答

0

JUnit中的初始化錯誤通常是由錯誤的類路徑引起的。請參閱此相關的問題也從初始化錯誤遭遇: Eclipse JUnit - possible causes of seeing "initializationError" in Eclipse window

最可能的原因是因爲這個問題解決,你正在使用一個版本的JUnit 4這就要求要添加的hamcrest罐子。除了添加junit和hamcrest jar之外,您應該能夠在項目的Java Build Path中添加JUnit 4庫。

您的導入很大程度上看起來很溫和,但您應該確認safe.SafetyException在您的類路徑中。

最後,初始化錯誤可能是由測試運行之前加載的代碼中的靜態初始化失敗引起的。你發佈的代碼看起來很安全,但SafetyException類可能有一個初始化塊來檢查。