3

Hi everybody my problem is the following: I want to write a testunit for a class i have written. For this I have the following Junit code:

  1 package ts;
  2 import ts.TransitionSystem;
  3 import ts.ConcreteTransitionSystem;
  4 import java.util.Set;
  5 import java.util.HashSet;
  6 import java.util.HashMap;
  7 
  8 import org.junit.Test;
  9 public class TransitionSystemTest {
 10 
 11   @Test
 12   public void addInitialTest () {
 13     TransitionSystem ts = new ConcreteTransitionSystem();
 14     ts.addInitialState("test");
 15     assertTrue(ts._initialStates.contains("test"));
 16     assertTrue(ts._states.get("s0").isEmpty());
 17   }
 18   
 19 }

my class looks as follows:

  1 package ts;
  2 
  3 import java.util.Set;
  4 import java.util.HashSet;
  5 import java.util.HashMap;
  6 import ts.TransitionSystem;
  7   
  8 public class ConcreteTransitionSystem implements TransitionSystem {
  9   Set<String> aps;
 10   HashMap<String, Set<String>> _states;
 11   Set<String> _initialStates;
 12 
 13   public void addInitialState(String s0) {
 14     Set<String> sucessors = new HashSet<String>();
 15     this._states.put(s0, sucessors);
 16     this._initialStates.add(s0);
 17   }
 18 }

and the error it get is the following:

TransitionSystemTest.java:15: error: cannot find symbol
    [javac]     assertTrue(ts._initialStates.contains("test"));
    [javac]                  ^
    [javac]   symbol:   variable _initialStates
    [javac]   location: variable ts of type TransitionSystem

and

TransitionSystemTest.java:16: error: cannot find symbol
    [javac]     assertTrue(ts._states.get("s0").isEmpty());
    [javac]                  ^
    [javac]   symbol:   variable _states
    [javac]   location: variable ts of type TransitionSystem

I cannot figure out the problem. Any ideas?

3 Answers3

3

Your initial problem is, that you attempt to access the classes internal variables via the TransitionSystem interface - you will of course have only access to the methods (and possibly constants) defined in your interface. This is really a good thing, as it separates the public contract (the interface) from the implementing class! And thus you should rethink your unit tests to use only the interface methods as well! The interface (along with wanted behaviour defined in some requirements document) defines the contract of the implementing class to the outer world, and anything you cannot test by its interface is just that: internal implementation detail of the implementing class).

Gyro Gearless
  • 5,131
  • 3
  • 18
  • 14
2

It's basic member scoping. ConcreteTransitionSystem._initialStates and _states are default or package level visibility. If you want to access them in this fashion, you could make them public or add getters (I'd recommend the latter). More here http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Taylor
  • 3,942
  • 2
  • 20
  • 33
0

Because the compiler thinks the variable ts has the type "TransitionSystem".

You can change the code to:

ConcreteTransitionSystem ts = new ConcreteTransitionSystem();
Y Lin
  • 1