Mockito Issue with Long and Lombok - mockito

How to write mockito for the below code, I went through: https://examples.javacodegeeks.com/core-java/junit/junit-mockito-when-thenreturn-example/
Code:
#Override
public void saveEmployee(EmployeeDto dto) {
Department department = getByDepartmentId(dto.getDepartmentId());
RoleType roleType = getByRoleTypeId(dto.getRoleTypeId());
Employee departmentMember = convertToEntity(dto, department, roleType);
try {
departmentMemberRepository.save(departmentMember);
} catch (DataIntegrityViolationException e) {
throw new PCDataIntegrityViolationException("error");
} catch (Exception ex) {
throw new InternalServerException(HttpStatus.INTERNAL_SERVER_ERROR, "error", ex);
}
}
private Employee convertToEntity(EmployeeDto dto, Department department, RoleType roleType) {
return Employee.pmBuilder()
.memberEmployeeId(dto.getMemberEmployeeId())
.memberEmployeeName(dto.getMemberEmployeeName())
.createUser(dto.getCreateUser())
.lastUpdateUser(dto.getLastUpdateUser())
.status(StatusEnum.get(dto.getStatus()))
.department(department)
.roleType(roleType)
.build();
}
private Department getByDepartmentId(Long departmentId) {
Optional<Department> optDepartment = departmentRepository.findById(departmentId);
if(!optDepartment.isPresent()) {
throw new ResourceNotFoundException("Error");
}
return optDepartment.get();
}
private RoleType getByRoleTypeId(Integer roleTypeId) {
RoleType roleType = roleTypeRepository.findByRoleTypeId(roleTypeId);
if(roleType == null) {
throw new ResourceNotFoundException("error");
}
return roleType;
}
I've written test class, only issue is that
#RunWith(PowerMockRunner.class)
#PrepareForTest({AHUtils.class })
public class EmployeeServiceTest {
#InjectMocks
private EmployeeServiceimpl employeeServiceimpl;
#Mock
private Pageable pageable;
#Mock
private Page<Employee> employeePage;
#Mock
private EmployeeRepository employeeRepository;
#Mock
private DepartmentRepository departmentRepositoryMock;
#Mock
private Employee employee;
#Mock
private Optional<Employee> employeeOptional;
#Mock
private Department departmentMock;
#Mock
private Optional<Department> departmentOptionalMock;
#Mock
private EmployeeDto employeeDto;
#Mock
private Sort sortMock;
#Mock
private Exception ex;
#Mock
private Environment env;
#Test(expected = ResourceNotFoundException.class)
public void test_RoleTypeNotPresent() {
when(departmentOptionalMock.get()).thenReturn(departmentMock);
when(departmentOptionalMock.isPresent()).thenReturn(true);
when(departmentRepositoryMock.findById(null)).thenReturn(departmentOptionalMock);
doThrow(new ResourceNotFoundException("error")).when(employeeRepository).save(any());
when(employeeDto.getDepartmentId()).thenReturn(null);
employeeServiceimpl.saveEmployee(employeeDto);
}
}
when Optional<Department> optDepartment = departmentRepository.findById(departmentId);, I wanted to have value in that so that I will go ahead, this testcase going inside if block.

In case your DepartmentRepository does not get injected into your EmployeeServiceimpl make sure of the following things
(Based on the javadoc of InjectMocks):
In case you have at least one constructor with arguments:(Mockito used constructor injection in that case)All mocks that are supposed to be injected must be parameters of your constructor with the largest number of arguments.Also note that mockito won't consider the other injection methods if there is a non no-args consturctor.
Second case would be you have setter method for the things you want to mock
(Mockito will use setter injection in that case)
In case you only have a no-args constructor and no setters:
(Mockito will use field injection in that case)All mocks annotated with #Mock have to share the same name as the fields in your EmployeeServiceimpl class.
Overmocking generally refers to the fact you generate too much mocks, even for things that are not needed to be mocks.
You could change the first 3 lines to what the example belows shows.
Note that I changed the .findById(null) to .findById(anyLong()).
I am not sure what getDepartmentId() actually returns, for a primitive long you have to use anyLong() as a mock would return 0 (and not null) by default.
However in the example below I set the departmentId to 1L so it should match regardless.
#RunWith(PowerMockRunner.class)
#PrepareForTest({AHUtils.class })
public class EmployeeServiceTest {
#InjectMocks
private EmployeeServiceimpl employeeServiceimpl;
#Mock
private DepartmentRepository departmentRepositoryMock;
#Test(expected = ResourceNotFoundException.class)
public void test_RoleTypeNotPresent() {
Department department = new Department();
department.setDepartmentId(1L);
// only if you can't simply create that object, use the mock
// Department department = Mockito.mock(Department.class);
when(departmentRepositoryMock.findById(anyLong())).thenReturn(Optional.of(department));
// ... the rest of the example does not match with the code you posted ...
employeeServiceimpl.saveEmployee(employeeDto);
}
}
Note that the remaining part of your test,
doThrow(new ResourceNotFoundException("error")).when(employeeRepository).save(any());
when(employeeDto.getDepartmentId()).thenReturn(null);
does not really match to the code you posted.
Instead your should define some behaviour on the mock of roleTypeRepository.
I am also not sure at what point a exception should be thrown, as there does not seem to be any interaction with an employeeRepository.

Related

How to create mockito unit tests in functions that use Mutiny.Session

After some investigation I continue to pursue a more intuitive way to mock Mutiny.Session and test my functionality.
Here the method I want to test:
public class ServiceCrud {
#Inject
Mutiny.SessionFactory mutinySessionFactory;
#Inject
MicroserviceService service;
public Uni<Service> get(#NotNull final String identifier) throws ConstraintViolationException, NotFoundException {
return mutinySessionFactory.withSession(session -> {
EntityGraph<Service> entityGraph = session.getEntityGraph(Service.class, "exampleEntityGraph");
return service.get(identifier)
.onItem().ifNull().failWith(NotFoundException::new)
.onItem().transformToUni(
microservice -> Uni.createFrom()
.item(new Service(microservice.getMsId(), microservice.getMsName())));
});
}
}
The only way I found to test was through the argument captor:
#ExtendWith(MockitoExtension.class)
public class ServiceCrudTest {
#InjectMocks
private ServiceCrud serviceCrud;
#Mock
Mutiny.SessionFactory mutinySessionFactory;
#Mock
MicroserviceService service;
#Captor
ArgumentCaptor<Function<Session, Uni<Service>>> captor;
#Mock
Session session;
#Mock
EntityGraph<Service> entityGraph;
#Test
void getTest() {
// data
String msId = "msid";
String msName = "name";
Microservice toBeReturned = new Microservice();
toBeReturned.setMsId(msId);
toBeReturned.setMsName(msName);
Service expected = new Service();
expected.setId(msId);
expected.setName(msName);
// expectation
doReturn(Uni.createFrom().item(toBeReturned))
.when(service).get(anyString());
doReturn(entityGraph).when(session)
.getEntityGraph(Mockito.eq(Service.class), Mockito.anyString());
// action
serviceCrud.get("");
// capture the function and invoke it
Mockito.verify(mutinySessionFactory).withSession(captor.capture());
Function<Session, Uni<Service>> value = captor.getValue();
Uni<Service> callback = value.apply(session);
UniAssertSubscriber<Service> subscriber = callback
.subscribe().withSubscriber(UniAssertSubscriber.create());
subscriber.assertCompleted().assertItem(expected);
}
}
Is it the only way to test? Is this the correct way to test?
Any tips will be appreciated

Mockito: How to test a class's void method?

Unit test noob here.
I have three classes: Db1Dao, Db2Dao, ExecuteClass where Db1Dao, Db2Dao are database access objects for two different databases. My goal is to fetch some data from db1 using Db1Dao and run executeClass.execute() to "put" the processed data into db2 using Db2Dao.
My ExecuteClass looks like this:
class ExecuteClass {
private Db1Dao db1Dao;
private Db2Dao db2Dao;
public void execute() {
...
List<String> listOfString = getExternalData(someParam);
List<Metadata> metadatum = db1Dao.get(someInputs);
... I do something to generate a list of new class `A` based on listOfString & metadatum ...
try {
db2Dao.put(listOfA);
} catch (PutException e){
...
}
}
public List<String> getExternalData(SomeClass someParam){
... do something
return listOfString;
}
}
Now I want to test:
Given a specific listOfString (returned by getExternalData) and a specific metadatum (returned by db1Dao.get):
Will I get the desired listOfA?
Am I able to call db2Dao.put and its input parameter is listOfA?
Particularly, I have hard-coded sample listOfString and metadatum and desired listOfA (and they will be passed via an object MockData, see the following code) but I don't know how to write the test using Mockito. The following is a test class I wrote but it does not work:
class TestClass extends BaseTest {
#Mock
private Db1Dao db1Dao;
#Mock
private Db2Dao db2Dao;
private ExecuteClass executeClass;
#BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
executeClass = new ExecuteClass(db1Dao, db2Dao);
}
#ParameterizedTest
#MethodSource("MockDataProvider")
public void executeClassTest(final MockData mockData) throws PutException {
Mockito.when(db1Dao.get(Mockito.any(), ...))
.thenReturn(mockData.getMetadatum());
ExecuteClass executeClassSpy = Mockito.spy(executeClass);
Mockito.when(executeClassSpy.getExternalData(Mockito.any()))
.thenReturn(mockData.getListOfString());
executeClassSpy.execute();
// executeClass.execute(); not working neither...
List<A> listOfA = mockData.getDesiredListOfA();
Mockito.verify(db2Dao).put(listOfA);
}
}
Could anyone please let me know? Thank you in advance!!
You should not create a spy of the same class you want to test. Instead, try to write a unit test for the smallest amount of code (e.g. a public method) and mock every external operator (in your case Db1Dao and Db2Dao).
If testing a public method involves calling another public method of the same class, make sure to mock everything inside the other public method (in your case getExternalData). Otherwise, this other public method might be a good candidate for an extra class to have clear separation of concerns.
So, remove the ExecuteClass executeClassSpy = Mockito.spy(executeClass); and make sure you setup everything with Mockito what's called within getExternalData.
To now actually, verify that Db2Dao was called with the correct parameter, either use your current approach with verifying the payload. But here it's important to 100% create the same data structure you get while executing your application code.
Another solution would be to use Mockito's #Captor. This allows you to capture the value of why verifying the invocation of a mock. Later on, you can also write assertions on the captured value:
#Captor
private ArgumentCaptor<ClassOfListOfA> argumentCaptor;
#Test
public void yourTest() {
Mockito.verify(db2Dao).put(argumentCaptor.capture());
assertEquals("StringValue", argumentCaptur.getValue().getWhateverGetterYouHave);
}
The following code worked for me.
I partially accepted #rieckpil's answer. I used #Captor which is very handy.
The reason I had to mock getExternalData() is because its implementation is still a "TODO".
class TestClass extends BaseTest {
#Mock
private Db1Dao db1Dao;
#Mock
private Db2Dao db2Dao;
#Captor
private ArgumentCaptor<List<A>> argumentCaptor;
private ExecuteClass executeClass;
#BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
executeClass = new ExecuteClass(db1Dao, db2Dao);
}
#ParameterizedTest
#MethodSource("MockDataProvider")
public void executeClassTest(final MockData mockData) throws PutException {
Mockito.when(db1Dao.get(Mockito.any(), ...))
.thenReturn(mockData.getMetadatum());
ExecuteClass executeClassSpy = Mockito.spy(executeClass);
Mockito.when(executeClassSpy.getExternalData(Mockito.any()))
.thenReturn(mockData.getListOfString());
executeClassSpy.execute();
List<A> listOfA = mockData.getDesiredListOfA();
Mockito.verify(db2Dao).put(argumentCaptor.capture());
assertEquals(listOfA, argumentCaptor.getValue());
}
}

thenReturn not returning expected values

#Component
public class RefValidator implements Component {
#Autowired
private Repository repository;
public void validate(Context context) {
Txn txn = context.getTxn();
if (Objects.nonNull(txn) && !StringUtils.isEmpty(txn.getReferenceNumber())){
if(txn.getId() == 0){
boolean isRealmIdAndReferenceNumberExists = repository.isRefNumberExistsInSale(txn.getRealmId(), txn.getReferenceNumber());
if(isRealmIdAndReferenceNumberExists){
throw new Exception();
}
}
}
}
}
I have a class as above and want to test it using mockito. I am doing #INjectMock for RefValidator and #Mock on Repository but when I do
Mockito
.when(repository.isRefNumberExistsInSale(Mockito.anyString(),Mockito.anyString()))
.thenReturn(true);
thenReturn doesn't return true.
It's kinda hard to define what's the problem because you haven't provided your test class. But it looks like your annotations are not being processed while running a test suit.
Try one of the following:
Add #RunWith(MockitoJUnitRunner.class) above your test class
Add MockitoAnnotations.initMocks(this); inside setup method (which is annotated with #Before)
See the 2nd section of this article for help.

How to mock the Data Stax Row object[com.datastax.driver.core.Row;] - Unit Test

Please find the below code for the DAO & Entity Object and Accessor
#Table(name = "Employee")
public class Employee {
#PartitionKey
#Column(name = "empname")
private String empname;
#ClusteringColumn(0)
#Column(name = "country")
private String country;
#Column(name = "status")
private String status;
}
Accessor:
#Accessor
public interface EmployeeAccessor {
#Query(value = "SELECT DISTINCT empname FROM EMPLOYEE ")
ResultSet getAllEmployeeName();
}
}
DAO getAllEmployeeNames returns a List which are employee names
and it will be sorted in ascending order.
DAO
public class EmployeeDAOImpl implements EmployeeDAO {
private EmployeeAccessor employeeAccessor;
#PostConstruct
public void init() {
employeeAccessor = datastaxCassandraTemplate.getAccessor(EmployeeAccessor.class);
}
#Override
public List<String> getAllEmployeeNames() {
List<Row> names = employeeAccessor.getAllEmployeeName().all();
List<String> empnames = names.stream()
.map(name -> name.getString("empname")).collect(Collectors.toList());
empnames.sort(naturalOrder()); //sorted
return empnames;
}
}
JUnit Test(mockito):
I am not able to mock the List[datastax row]. How to mock and returns a list of rows with values "foo" and "bar".Please help me in unit test this.
#Category(UnitTest.class)
#RunWith(MockitoJUnitRunner.class)
public class EmployeeDAOImplUnitTest {
#Mock
private ResultSet resultSet;
#Mock
private EmployeeAccessor empAccessor;
//here is the problem....how to mock the List<Row> Object --> com.datastax.driver.core.Row (interface)
//this code will result in compilation error as we are mapping a List<Row> to the ArrayList<String>
//how to mock the List<Row> with a list of String row object
private List<Row> unSortedTemplateNames = new ArrayList() {
{
add("foo");
add("bar");
}
};
//this is a test case to check if the results are sorted or not
//mock the accessor and send rows as "foo" & "bar"
//after calling the dao , the first element must be "bar" and not "foo"
#Test
public void shouldReturnSorted_getAllTemplateNames() {
when(empAccessor.getAllEmployeeName()).thenReturn(resultSet);
when(resultSet.all()).thenReturn(unSortedTemplateNames); //how to mock the List<Row> object ???
//i am testing if the results are sorted, first element should not be foo
assertThat(countryTemplates.get(0), is("bar"));
}
}
Wow! This is overly complex, hard to follow, and not an ideal way to write unit tests.
Using PowerMock(ito) along with "static" references in your own code is not recommended and is a sure sign of a code smells.
First, I am not sure why you decided to use a static reference (e.g. EmployeeAccessor.getAllEmployeeName().all(); inside the EmployeeDAOImpl class, getAllEmployeeNames() method) instead of using the instance variable (i.e. empAccessor), which is more conducive to actual "unit testing"?
The EmployeeAccessor, getAllEmployeeName() "interface" method is not static (clearly). However, seemingly, whatever this (datastaxCassandraTemplate.getAccessor(EmployeeAccessor.class);) generates makes it so (really?), which then requires the use of PowerMock(ito), o.O
Frameworks like PowerMock, and extensions of (i.e. "PowerMockito"), were meant to test and mock code used by your application (unfortunately, but necessarily so) where this "other" code makes use of statics, Singletons, private methods and so on. This anti-pattern really ought not be followed in your own application design.
Second, it is not really apparent what the "Subject Under Test" (SUT) is in your test case. You implemented a test class (i.e. EmployeeDAOImplTest) for, supposedly, your EmployeeDAOImpl class (the actual "SUT"), but inside your test case (i.e. shouldReturnSorted_getAllTemplateNames()), you are calling... countryLocalizationDAOImpl.getAllTemplateNames(); thus testing the CountryLocalizationDAOImpl class (??), which is not the "SUT" of the EmployeeDAOImplTest class.
Additionally, it is not apparent that the EmployeeDAOImpl even uses a CountryLocalizationDAO instance (assuming an interface here as well), and if it does, then it is certainly something that should be "mocked" when the EmployeeDAOImpl "interacts" with instances of CountryLocalizationDAO, particularly in the context of a unit test. The only correlation between the EmployeeDAO and CountryLocalizationDAO is that the Employee has a country field.
There are a few other problems with your design/setup as well, but anyway.
Here are a few suggestions...
First, let's test what your EmployeeDAOImplTest is meant to test... EmployeeDAO.getAllEmployeeNames() in a sorted fashion. This in turn may give you ideas of how to test your "CountryLocalizationDAO, getAllTemplateNames() method perhaps (if it even makes sense, i.e. getAllTemplateNames() is in fact dependent on an Employee's country, when Employees are ordered by name (i.e. "empname" and accessed via EmployeeAccessor).
public class EmployeeDAOImpl implements EmployeeDAO {
private final EmployeeAccessor employeeAccessor;
// where does the DataStaxCassandraTemplate reference come from?!
private DataStaxCassadraTemplate datastaxCassandraTemplate = ...;
public EmployeeDAOImpl() {
this(datastaxCassandraTemplate.getAccessor(EmployeeAccessor.class));
}
public EmployeeDAOImpl(EmployeeAccessor employeeAccessor) {
this.employeeAccessor = employeeAccessor;
}
protected EmployeeAccessor getEmployeeAccessor() {
return this.empAccessor;
}
public List<String> getAllEployeeNames() {
List<Row> nameRows = getEmployeeAccessor().getAllEmployeeName().all();
...
}
}
Then in your test class...
public class EmployeeDAOImplUnitTest {
#Mock
private EmployeeAccessor mockEmployeeAccessor;
// SUT
private EmployeeDAO employeeDao;
#Before
public void setup() {
employeeDao = new EmployeeDAOImpl(mockEmployeeAccessor);
}
protected ResultSet mockResultSet(Row... rows) {
ResultSet mockResultSet = mock(ResultSet.class);
when(mockResultSet.all()).thenReturn(Arrays.asList(rows));
return mockResultSet;
}
protected Row mockRow(String employeeName) {
Row mockRow = mock(Row.class, employeeName);
when(mockRow.getString(eq("empname")).thenReturn(employeeName);
return mockRow;
}
#Test
public void getAllEmployeeNamesReturnsSortListOfNames() {
when(mockEmployeeAccessor.getAllEmployeeName())
.thenReturn(mockResultSet(mockRow("jonDoe"), mockRow("janeDoe")));
assertThat(employeeDao.getAllEmployeeNames())
.contains("janeDoe", "jonDoe");
verify(mockEmployeeAccessor, times(1)).getAllEmployeeName();
}
}
Now, you can apply similar techniques if in fact there is an actual correlation between Employees and CountryLocalizationDAO via the EmployeeAccessor.
Hope this helps get you on a better track!
-j

How to Mock repository calls ATG

Can we use Mockito to write tests for methods which implements repository calls? For example below method is using Named query to get eBooks from a Book Repository -
public RepositoryItem[] getEBooks(DynamoHttpServletRequest request) {
RepositoryItem[] results = null;
Repository rep = (Repository) request.resolveName("/atg/products/BookRepository");
try {
RepositoryItemDescriptor desc = rep.getItemDescriptor("Book");
RepositoryView view = desc.getRepositoryView();
if (view instanceof NamedQueryView) {
NamedQueryView nameView = (NamedQueryView) view;
ParameterSupportView pSupportView = (ParameterSupportView) view;
String queryName = "GetBooks";
Query namedQuery = nameView.getNamedQuery(queryName);
Object[] params = { "ebook" }; //book type
results = pSupportView.executeQuery(namedQuery, params);
}
} catch (RepositoryException e) {
logError(e.getMessage());
}
return results;
}
Thanks.
Yes you can. The question is though are you testing YOUR code or ATG in this instance?
Assuming your method above is contained in a class called GetBooks your test could look something like this:
#InjectMocks private GetBooks testObj;
#Mock private DynamoHttpServletRequest requestMock;
#Mock private Repository bookRepositoryMock;
#Mock private RepositoryItemDescriptor bookRepositoryItemDescriptorMock;
#Mock private GSAView bookRepositoryViewMock; //The only oddity here but GSAView is the common denominator for NamedQueryView and ParameterSupportView
#Mock private Query namedQueryMock;
#Mock private RepositoryItem resultRepositoryItem1, resultRepositoryItem2;
#BeforeMethod(groups = { "unit" })
public void setup() throws Exception {
testObj = new GetBooks();
MockitoAnnotations.initMocks(this);
Mockito.when(requestMock.resolveName("/atg/products/BookRepository")).thenReturn(bookRepositoryMock);
Mockito.when(bookRepositoryMock.getItemDescriptor("Book")).thenReturn(bookRepositoryItemDescriptorMock);
Mockito.when(bookRepositoryItemDescriptorMock.getRepositoryView()).thenReturn(bookRepositoryViewMock);
Mockito.when(bookRepositoryViewMock.getNamedQuery("GetBooks")).thenReturn(namedQueryMock);
List<RepositoryItem> resultArrayList = new ArrayList<RepositoryItem>();
resultArrayList.add(resultRepositoryItem1);
resultArrayList.add(resultRepositoryItem2);
Object[] params = { "ebook" }; //It may be simpler to test if this was a constant
Mockito.when(bookRepositoryViewMock.executeQuery(namedQueryMock, params)).thenReturn(resultArrayList.toArray(new RepositoryItem[resultArrayList.size()]));
}
#Test(groups = { "unit" })
public void testGetEBooks()throws Exception{
RepositoryItem[] result = testObj.getEBooks(requestMock);
Assert.assertTrue(result.length == 2); //What do you want to test?
}
This gives a greenbar when executed via TestNG. But what are you really testing here?
On a separate note. You should really use (protected) constants more since you'll then be able to use them in the package scope of your Mockito tests.

Resources