I'm starting some testing using Mockito on some service classes I use for connecting to my data store. I now want to determine the best practice way writing tests for it. The principle is for each entity there is a way to list, add, delete etc a row from the data store(mongo/mysql etc) for a specific entity.
Take this class that allows me to talk to my database that stores a list of companies
public class CompanyService extends Service{
public CompanyService() {
...
}
public saveCompany(Company company) {
...
}
// get a list of all companies
public List<Company> getCompanies() {
List<Company> companies = new ArrayList<Company>();
try {
CompanyResult<Rows<String, String>> rows = db.query(....);
for (Row<String, String> row : rows.getResult()) {
companies.add(row.getColumns());
}
catch (Exception e){
logger.warn("Error retrieving companies", e);
}
}
}
What exactly should I test in the getCompanies method and how can I use Mockito to do it?
Your System Under Test is the Company Service. You want to test that, assuming all its dependencies/collaborators function properly, it functions properly.
The db object looks like the only dependency/collaborator you need to worry about within the getCompanies() method. Use Mockito to mock the call to db.query()
You could set up a test method like so:
#Test
public void testGetCompanies() {
/*** Arraign ***/
CompanyService cs = new CompanyService();
// Setup mock db
DB mockDb = mock(DB.class);
// Setup fake results for query
CompanyResult<Rows<String, String>> sampleResults = ... // build sample results here
// Have query on mock return fake results
when(db.query(/* match arguments */)).thenReturn(sampleResults);
// Tell your System Under Test to use the mock collaborator
cs.setDB(mockDb);
/*** Act ***/
CompanyResult<Rows<String, String>> results = cs.getCompanies();
/*** Assert ***/
... // Test that results and sampleResults are effectively the same
}
You could test how your code would work if the db query returned and empty result,null or if row has null values or unexpected values. You could mock the db class to return these values. You could also mock the db class to throw an exception to see how your code could react.
Related
I am trying to test my method for getting back all entities that exist in my db. I am using JUnit and Mockito. I have no experience with testing so far and this is how far I've got:
This is my method from the agency service to get back all entities, using the findAll() function of JpaRepository:
public List<AgencyDto> getAll() {
return repo.findAll().stream().map(agency -> mapper.mapToDto(agency)).collect(Collectors.toList());
}
#ExtendWith(MockitoExtension.class)
public class AgencyServiceTest {
#Mock
private AgencyRepository agencyRepository;
#InjectMocks
private AgencyService agencyService;
#Test
void getAgencies() {
List<Agency> existingAgencies = new ArrayList<Agency>();
Agency agency1 = new Agency();
Agency agency2 = new Agency();
existingAgencies.add(agency1);
existingAgencies.add(agency2);
when(agencyRepository.findAll()).thenReturn(existingAgencies);
List<AgencyDto> result = agencyService.getAll();
assertEquals(existingAgencies, result);
}
}
When running the test, the value for expected seems ok, but the value for actual is an empty array:
Expected :[com.project.DTOs.AgencyDto#245a26e1, com.project.DTOs.AgencyDto#4d63b624, com.project.DTOs.AgencyDto#466cf502]
Actual :[]
Is this not the right way to test get() methods? Am I doing something wrong when setting the actual result?
if i understand your code coreectly
agencyRepository.findAll() return List<Agency>
agencyService.getAll() return List<AgencyDto>
Issue is here
List<Agency> existingAgencies = new ArrayList<Agency>();
when(agencyRepository.findAll()).thenReturn(existingAgencies);
your mock returns an empty list, you need to add items to list
eg:
List<Agency> existingAgencies = new ArrayList<Agency>();
existingAgencies.add(agencyObjectInDB1);
existingAgencies.add(agencyObjectInDB2);
existingAgencies.add(agencyObjectInDB3);
//Mock repo call
when(agencyRepository.findAll()).thenReturn(existingAgencies);
//I assume Here inside getAll() , agencyRepository.findAll() is invoked
//getAll() converts Agency to AgencyDto
List<AgencyDto> result = agencyService.getAll();
// assert against pre-pepared list
assertEquals(agencies, result);
I am using Quarkus with Hibernate-ORM PanacheRepository and I need to mock the PanacheQuery.
I have the following classes:
Label(JPA Entity)
LabelRepository (implements PanacheRepository< Label > {})
In my test class I need to mock the labelRepository.find("name", name). This method return a PanacheQuery but I don't know how I can create a new PanacheQuery mock.
#QuarkusTest
class LabelResourceTest {
#Inject LabelResource labelResource;
#InjectMock LabelRepository labelRepository;
private Label label;
private List<Label> labels;
#BeforeEach
void setUp() {
label = new Label();
label.setId(1L);
label.setName("LABEL#01");
label.setInheritable(true);
labels = new ArrayList<>();
labels.add(label);
}
#Test
void getNameTest() {
when(labelRepository.find("name", "LABEL#01")).thenReturn(......);
.....
}
}
Thank you.
The final working piece of code (thanks to #loicmathieu):
PanacheQuery query = Mockito.mock(PanacheQuery.class);
Mockito.when(query.page(Mockito.any())).thenReturn(query);
Mockito.when(query.firstResultOptional()).thenReturn(Optional.of(label));
when(labelRepository.find("name", "LABEL#01")).thenReturn(query);
I my case I have used the firstResultOptional() method but you can replace it if you need to use the list() because you are getting the entire list and not only a single item.
This is a very good point !
Today, there is no easy way to mock a PanacheQuery. Depending on the Panache flavor, a PanacheQuery is backed with a JPA Query (for Hibernate) or a BSON Query (for MongoDB) and have the capability to act on this query object (for example, paginate).
As of today, you may need to create a Mock on the PanacheQuery interface and return this mock on your when(labelRepository.find("name", "LABEL#01")).thenReturn(......);.
Assuming you're using only the PanacheQuery.page() and the PanacheQuery.list() methods something like this (not tested should work):
PanacheQuery query = Mockito.mock(PanacheQuery.class);
Mockito.when(query.page(Mockito.any()).thenReturn(query);
Mockito.when(query.list()).thenReturn(labels);
I'll open an issue on Quarkus, maybe we can do better (provide a MockQuery) or maybe we should at least document this.
Another solution would be to encapsulate the various calls you make to the PanacheQuery object in a dedicated method inside your entity and mock this one like in this example: https://quarkus.io/guides/hibernate-orm-panache#adding-entity-methods
I am writing unit tests for fetching records from Oracle DB using JOOQ library and I need to mock data returned by DSLContext's fetch() function. How can I create sample Result<Record> to be returned by mocked function? I googled it for few hours and could not find an answer.
Try to use JOOQ's own mock API. Here are the official docs
You probably want to end up with something like that:
final MockDataProvider myMockProvider = new MockDataProvider() {
#Override
public MockResult[] execute(final MockExecuteContext context) throws SQLException {
final DSLContext context = DSL.using(SQLDialect.ORACLE);
final Result<Record> resultRecord = context.newResult(YOUR_TABLE_HERE);
// customize your record with needed fields
resultRecord.add(context.newRecord(YOUR_TABLE_HERE));
return new MockResult[] { new MockResult(1, resultRecord) };
}
};
final DSLContext mockedDSL = DSL.using(new MockConnection(myMockProvider), SQLDialect.ORACLE);
// here you go with your tests
I've faced the same issue, and I didn't want to have a MockDataProvider, as I was testing something else than the DAO. Therefore, I created a trivial function in order to convert a Record (or multiple) into a Result<T>.
Note that this is in Kotlin, but it should be easy to translate this into Java:
val jooq = DSL.using(SQLDialect.POSTGRES)
fun <T : Record> result(table: TableImpl<T>, vararg data: T): Result<T> {
return jooq.newResult(table).apply { addAll(data) }
}
Which then can be used as follows:
result(TABLE_NAME, <a record>, <another record>)
And records can just be created using their constructors.
For school purposes I am creating an application that's working with a stock API.
I am trying to write a test for a method that gets all the stock data of the last 10 years. Instead of actually getting all that data, I want to throw an exception.
The method I Want to test:
#Override
public List<StockData> getAllTeslaStockData() throws AlphaVantageException {
List<StockData> stockData;
AlphaVantageConnector apiConnector = new AlphaVantageConnector(APIKEY, TIMEOUT);
TimeSeries stockTimeSeries = new TimeSeries(apiConnector);
try {
Daily responseDaily = stockTimeSeries.daily("TSLA", OutputSize.FULL);
stockData = responseDaily.getStockData();
} catch (AlphaVantageException e) {
LOGGER.log(Level.SEVERE, "something went wrong: ", e);
throw e;
}
return stockData;
}
The stockTimeSeries.daily(....) call can throw the AlphaVantageException.
I've mocked the TimeSeries class like this:
TimeSeries stockTimeSeries = mock(TimeSeries.class);
In my test class I want to mock this call, and return an exception instead of actual data.
when(stockTimeSeries.daily("TSLA", OutputSize.FULL)).thenThrow(new AlphaVantageException("No stock data available"));
Regardless of how I am trying to mock this bit of code, it'll never throw the exception. It will always just execute the code, and return valid stock data, instead of throwing the exception like i've tried to do.
How can I mock this bit of code, so that itll throw the exception I am expecting for my tests.
The AlphaVantageConnector, TimeSeries and Daily classes are part of a library used to access the stock API, so I can't change these classes.
I am using JUnit 4.12 and Mockito to try and achieve this.
You can use thenThrow() method. Below is the example
#Test(expected = NullPointerException.class)
public void whenConfigNonVoidRetunMethodToThrowEx_thenExIsThrown() {
MyDictionary dictMock = mock(MyDictionary.class);
when(dictMock.getMeaning(anyString()))
.thenThrow(NullPointerException.class);
dictMock.getMeaning("word");
The TimeSeries object is created in the method itself, so you can't mock it - mocking is intended to mock members.
What you can do is to do something like
class YourClass {
private Supplier<TimeSeries> seriesCreator = () -> {
return new TimeSeries(new AlphaVantageConnector(APIKEY, TIMEOUT));
}
which you use to create the series in your method
#Override
public List<StockData> getAllTeslaStockData() throws AlphaVantageException {
TimeSeries stockTimeSeries = seriesCreator.get();
Now you can mock that Supplier.
#Mock Supplier<TimeSeries> seriesCreatorMock;
#InjectMocks MyClass sut;
and in your test
#Test(expected = AlphaVantageException.class)
void testException() {
when(seriesCreatorMock.get()).thenThrow(new AlphaVantageException());
sut.getAllTeslaStockData()
}
EDIT: as suggested by Angkur in the comments, the clean way would be to
class SeriesCreator implements Supplier<TimeSeries> {
public TimeSeries get() {
return new TimeSeries(new AlphaVantageConnector(APIKEY, TIMEOUT));
}
}
class YourClass {
private Supplier<TimeSeries> seriesCreator = new SeriesCreator();
// ...
The code in the main class is creating a new instance of TimeSeries which it will use every time this method is called, so the mocked TimeSeries object is not getting used at all.
TimeSeries stockTimeSeries = new TimeSeries(apiConnector); // --> This is not getting mocked
try {
Daily responseDaily = stockTimeSeries.daily("TSLA", OutputSize.FULL);
stockData = responseDaily.getStockData();
}
You should create another method in your class (or even a separate class if it better satisfies the SOLID principles) which returns you the TimeSeries object. Something like:-
<access modifier> TimeSeries getTimeSeries(...) {
}
and then this method should be mocked in the Junit, and when mocked, it should return the Mocked TimeSeries reference (which is created in TimeSeries stockTimeSeries = mock(TimeSeries.class); ). You would need to use .spy() on the main class (unless you are using a different class to create TimeSeries object) in order to be able to mock the specific method getTimeSeries() but not the others.
MainClass mainObject = Mockito.spy(new MainClass());
Mockito.when(mainObject.getTimeSeries()).thenReturn(stockTimeSeries);
Then, the method call stockTimeSeries.daily() will get actually mocked by your existing code :
when(stockTimeSeries.daily("TSLA", OutputSize.FULL)).thenThrow(new AlphaVantageException("No stock data available"));
NOTE: you should also consider using .anyString() style methods provided by Mockito API while mocking.
Let's say I have the following classes in the respective source folders/packages...
[src/myApp]
|_Employee «concrete»
|_Manager «abstract»
|_ManagerImpl «concrete» (Class Under Test)
|_Recruiter «abstract»
|_RecruiterImpl «concrete» (Collaborator)
...
public class ManagerImpl implements Manager {
...
private Recruiter recR;
...
public void growTeam( Object criteria ){
//...check preconditions
Employee newB = recR.srcEmployee( criteria );
//...whatever else
}
...
}
...
[test/myApp]
|_RecruiterStandIn «concrete»
|_ManagerImplTest
...
public class RecruiterStandIn implements Recruiter {
Map<Object, Employee> reSrcPool = new HashMap<>();
public RecruiterStandIn( ){
// populate reSrcPool with dummy test data...
}
public Employee srcEmployee( Object criteria ){
return reSrcPool.get( criteria );
}
}
...
public class ManagerImplTest {
...
// Class Under Test
private ManagerImpl mgr;
// Collaborator
private Recruiter recR = new RecruiterStandIn( );
...
public void testGrowTeam( ) {
//...
mgr.setRecruiter( recR );
mgr.growTeam( criteria );
// assertions follow...
}
...
}
...
Here are my questions: Given that I have a RecruiterStandIn concrete implementation that already exists within the codebase for testing purposes (in the test scope)...
Would it be redundant to also use a mock in the above unit test?
What would be the value (if any) in additionally doing something like this in the above unit test?
...
...
#Mock
private Recruiter recR;
...
...
public void testGrowTeam( ) {
...
expect( recR.srcEmployee( blah) ).andReturn( blah )...
// exercising/assertions/validations as usual...
}
...
You can safely assume that RecruiterStandIn does everything the class under test will ever require of it for the purposes of the above unit test. That is to say, for the sake of simple answers/explanations, there's no need to overcomplicate the above scenario with contrived what-ifs around maintenance of the stub and whatnot.
Thanks in advance.
My answers to your specific questions:
Would it be redundant to also use a mock in the above unit test?
As the unit test is written right now it would be redundant but my see answer to your second question below.
What would be the value (if any) in additionally doing something like this in the above unit test?
It think this is the way you should be doing your tests and I would recommend getting rid of your stub, RecruiterStandIn. Instead I would setup the recruit to return canned answers so you don't have to maintain two classes just to return some predefined data:
#Spy
private Recruiter recR;
public void testGrowTeam( ) {
// Setup canned data return
doReturn(generateTestEmployee()).when(recR).srcEmployee(any(Object.class));
expect( recR.srcEmployee( blah) ).andReturn( blah )...
// exercising/assertions/validations as usual...
}
FYI the above syntax would be for using Mockito. From what I can tell in your case Mockito gives you the power of stubbing out certain parts and much more without requiring you to create new test entities.
Original Answer
Definitely yes you should be doing mock object tests. Mocks Aren't Stubs. Mock object testing allows you to test the interactions between your classes and ensure that things are interacting with the world around them correctly. I think there is less value in these tests when you first write the classes and their corresponding tests. Mock object tests shine a year down the road when a new developer comes in and inadvertently your breaks code because she didn't understand that certain internal behavior was needed..
A somewhat contrived example would be if let's say we had a Car that needed to fill up some gas:
public class Car {
public void fuelUp()
}
Now with standard unit tests we would check that after calling fuelUp() the car was full of gas and that the proper amount of money was deducted from the driver. But since we never tested how fuelUp() was interacting with the world around it, it could easily be doing the following:
public void fueldUp() {
siphonGasFromNearestCar();
buyCoffeeAndChips()
}
But with mock object testing you can ensure that the Car is getting filled up in the proper and expected way.