2012-01-08 17 views
1

こんにちは、私はjunitを使用しようとしていますが、うまくいきません。junit test with Eclipse

ここに私のコードです。

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として実行すると、初期化エラーが発生します。私はそれが助けば日食で働き、私はJunitをプロジェクトの図書館に入れます。

+0

音がクラスパスの問題のようです。 –

+0

junit.rarをクラスパス – user1088557

+0

に追加します。それでは問題は何ですか? –

答えて

0

JUnitで初期化エラーが発生するのは、一般に、悪いクラスパスが原因です。 Eclipse JUnit - possible causes of seeing "initializationError" in Eclipse window

最も可能性が高い原因は、hamcrest jarを追加する必要があるJUnit 4のバージョンを使用していることでしょう。 junitとhamcrest jarを追加する代わりに、プロジェクトのJava Build PathにJUnit 4ライブラリを追加することができます。

あなたの輸入は大丈夫ですが、safe.SafetyExceptionがクラスパスにあることを確認する必要があります。

最後に、テストが実行される前にロードされるコードの静的初期化エラーによって初期化エラーが発生する可能性があります。投稿したコードは安全に見えますが、SafetyExceptionクラスは初期化ブロックをチェックする可能性があります。

関連する問題