I am new writing good test cases, so please bear with me. Writing a test case for private methods
public Stock getStock(String stockTicker) {
Stock company = new Stock();
String url = getEndpointURL(stockTicker);
htmlDocument = fetchHTMLDocument(url); // private method
company.name = getStockName(); // private method uses htmlDocument
company.actualPrice = getActualPrice(); // private method uses htmlDocument
return company;
}
Current Test case looks like
assertThat(selector.getActualPrice()).isGreaterThan(-1);
assertThat(selector.getStockName()).isNotNull();
But this doesn't allow me to make methods private..
To make the methods private, and since I want to test getStock
only using public methods (like recommended in "How do you unit test private methods?"), I want to write test case for getStock
method as below
Stock st = selector.getStock("AAPL");
assertThat(st.name).isNotNull();
i.e make assertions on Stock
object
However, in this test, I want to avoid the backend call made by fetchHTML()
and pass a custom Document to test the functioning of getStockName()
and getActualPrice()
method.
How do I do this without making methods public?
htmlDocument
? it would be good if you could fix de example. Right now these methods look blind and totally opaque. On the other hand, doesfetchHTMLDocument
has any dependency you can mock/stubb?htmlDocument
and it is internal member of class stock selector