So I have a class structure setup like so:
public class foo {
private DomainService service;
//constructor to initialize
public Fleet someMethod(Car car) {
Fleet fleet = new Fleet();
if(car!=null) {
Plane plane = service.findObjectByID(car);
plane.setColor(Blue);
fleet.addPlane(plane);
}
return fleet;
}
}
public class DomainService {
List<Plane> planes;
//constructure to initialize
public Optional<Plane> findObjectByID(Car car) {
for(Plane plane : planes) {
if(plane.getID() == car.getID()) {
return Optional.of(plane);
}
}
return Optional.absent();
}
}
public class Fleet {
List<Plane> planes;
int ID;
Date serviceDate;
}
When the user logs into the application, there are a lot of operations and calculations that happen via spring managed beans. They pull all the information about the user from a database and store it into objects. In the above example, when the user logs the list of planes is populated from their information that is pulled from the DB.
My problem is JUnit testing this. My JUnit looks something like this:
public class fooTest {
private Foo foo;
private Car car;
@Before
public void setUp() throws Exception {
foo = new Foo();
}
@Test
public void isBlue() {
car = new Car;
car.setID(123);
Fleet result = foo.someMethod(car);
assertEquals(Color.BLUE, result.getColor());
}
}
The problem is the planes objects get initialized when the user first logs in, including the plane IDs. Which are real world IDs that are a string of random numbers and characters. Since this is a test the planes aren't getting populated with anything real. And even if it was populated with real world data, I would have no idea what ID to set it as because they're random.
So every time I try to execute this JUnit it fails because findObjectByID always returns a null.
I was looking into mocking or ArgumentCaptor but I'm new to JUnit testing and don't really know how those work or if they could help in my situation. Any ideas on how to get around this issue?
Aucun commentaire:
Enregistrer un commentaire