I am writing some JUnit tests for a bus stop but I am having trouble trying to create objects to be used through the tests:
public class StopTests {
BusRoute rte = new BusRoute("250");
Set<BusRoute> set = new Set<BusRoute>();
BusStop stop = new BusStop(00000, "Staples Center", 90.0, 90.0, set);
...
}
My problem is that the test won't compile because Eclipse says "new Set()" cannot be instantiated. My intention is to add 'rte' into 'set' so that 'stop' can be created without compilation errors, but I am stumped on how to do this. I tried approaching it like:
public class StopTests {
BusRoute rte = new BusRoute("250");
Set<BusRoute> set = new Set<BusRoute>();
set.add(BusRoute rte);
BusStop stop = new BusStop(00000, "Staples Center", 90.0, 90.0, set);
...
}
but Eclipse gave me another error about not having an identifier following 'add'.
What would be the best way to approach this?
EDIT: This is what I have now:
public class StopTests {
BusRoute rte = new BusRoute("250");
Set<BusRoute> set = new HashSet<BusRoute>();
set.add(rte);
BusStop stop = new BusStop(00000, "Staples Center", 90.0, 90.0, set);
...
}
Set
is an interface so cannot create an instance of it. You need to create and instance of Set implementations such as HashSet
.
Try changing this:
Set<BusRoute> set = new Set<BusRoute>()
to
Set<BusRoute> set = new HashSet<BusRoute>()
Also to add the element in set you need to call add
on set instance and not on BusRoute
instance. So change this :
rts.add(BusRoute rte);
to
set.add(rte);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments