I have been building a couple test cases with EasyMock. In doing so I have a class called Portfolio and one of its data members is an object called Stock.
I am currently trying to create a partial Mock for one method (validateStock) in Portfolio. Here is the mock creation in my test file:
//Create a partial mock of the object we are testing
Portfolio portfolio = EasyMock.partialMockBuilder(Portfolio.class)
.addMockedMethod("validateStock").createMock();
My tests fail because when running them stock is null and I need it to not be. It only passes if I add this after creating the mock in my testfile.
//create a Stock object that we will set to portfolio and set it.
Stock stock = new Stock();
portfolio.setStock(stock);
The reason this confuses me is because in my Portfolio class I am both declaring and initializing a Stock object:
public class Portfolio {
private Stock stock = new Stock(); ...}
Can anyone tell me why I have to manually make a Stock object and set it through my partial mock portfolio object? Shouldn't it make a new one on its own?
I have been unable to find any resources online addressing that specifically. I am leaning towards the answer being about how EasyMock is using reflection and perhaps reflection will not also create the datamembers of classes if they are objects themselves. Will continue to research but if anyone can point me in the right direction I would appreciate it.
Aucun commentaire:
Enregistrer un commentaire