Simon
Simon

Reputation: 5

Can't change accessibility to field in unit test . getDeclaredField can't find my field

I have the problem that my getDeclaredField can't find my field, and I can't find why. Any ideas?

public interface MapInterface<K extends Comparable<K>, V> {

    public void setValue(K key, V value);

    public V getValue(K key);

}

This is the class that implements that interface:

public class RbtMap<K extends Comparable<K>, V> implements MapInterface<K, V> {

    private final RedBlackTree<K, V> tree;

    public <K, V> RbtMap() {
        tree = new RedBlackTree<>();
    }

In my unit test, I try get access to tree

Field treeField = RbtMap.class.getDeclaredField("tree");

but I have that:

Unhandled exception: java.lang.NoSuchFieldException

I don't know what I should do.

Upvotes: 0

Views: 52

Answers (1)

Anubhav Sharma
Anubhav Sharma

Reputation: 543

add throws NoSuchFieldException, SecurityException to method signature like below

@Test
public void test() throws NoSuchFieldException, SecurityException{
        Field treeField = RbtMap.class.getDeclaredField("tree");
        System.out.println(treeField.getName());
}

Upvotes: 0

Related Questions