JUnit, refleksja

0

Napisałem sobie taką klasę

package pl.core;
public class Sorter {
    
    /**
     * Store min object (index, value)
     */
    class Min {
        private int value;
        private int index;
        
        /**
         * Create object min, and set its value and index
         * @param v value of min
         * @param i index of min
         */
        Min(int v, int i) {
            this.value = v;
            this.index = i;
        }
        //reszta funckjo
    private Min getMin(List<Integer> tab) {
        int min_index, min_value;
        min_value = tab.get(0);
        min_index = 0;
        
        for (int i = 1; i < tab.size(); i++) {
            if (tab.get(i) < min_value){
                min_value = tab.get(i);
                min_index = i;
            }
        }
        Min min = new Min(min_value, min_index);
        return min;
    }
}

Chciałbym teraz przetestować czy funkcja getMin działa poprawnie

public void testGetMin(){
        Object minValue;
        List<Integer> tab = new ArrayList<Integer>();
        Sorter sorter = new Sorter();
        
        Sorter.Min min = sorter.new Min(2,3);
        
        tab.add(8);
        tab.add(4);
        
        //int parameter
	Class[] paramList = {List.class};	
        
        try {
            Class mySorter = Class.forName("pl.core.Sorter");
            Object Sorter = mySorter.newInstance();
               
            Method method = mySorter.getDeclaredMethod("getMin", paramList);
            method.setAccessible(true);
            minValue = method.invoke(Sorter, tab);
            System.out.print(minValue);   //
            
        } catch (Exception e) {
            System.out.println(e);
        }
    }

Niestety System.out.print(minValue); zwraca pl.core.Sorter$Min@2e2e06bd------------- ---------------- ---------------
Czy jest jakiś sposób aby wynikiem method.invoke(Sorter, tab); był obiekt klasy Min?
Jak zobaczyć jakie zmienne są w obiekcie minValue?

1

Napisz w klasie Min metodę public String toString(), która zwróci taki opis obiektu jaki jest Ci potrzebny. Np.

public String toString()
{
    return "value = "+value+" index = "+index; 
}
0

Proponuje używać sensownych narzędzi, w tym przypadku uzyj np. Whiteboxa z Powermocka.

1 użytkowników online, w tym zalogowanych: 0, gości: 1