How to Mock jdbcTemplate.getJdbcTemplate().execute(); method? - spring

My question is how to mock
jdbcTemplate.getJdbcTemplate().execute("TRUNCATE table TABLE_1");
method?
It's return value is void if I am right.
#RunWith(MockitoJUnitRunner.class)
public class Test {
#Mock
private NamedParameterJdbcTemplate jdbcTemplate;
...
//I tried this but of course it doesn't work
doReturn(void).when(jdbcTemplate).getJdbcTemplate().execute("TRUNCATE TABLE TABLE_1");
Any help is welcome.

From the comments, my understanding is that getJdbcTemplate is not final and you're interested in mocking its return value so you can verify method calls.
Here's the approach I'd take:
// create a mock for the return value
#Mock
JdbcTemplate template;
// create a mock for the factory
#Mock
NamedParameterJdbcTemplate jdbcTemplate;
#Before
public void setUp() {
// other initializations here
// configure the factory to return the above mocked template
when(jdbcTemplate. getJdbcTemplate()).thenReturn(template);
}
Now you should be able to verify on the template mock:
verify(template).execute("TRUNCATE TABLE TABLE_1");

Related

Junit and mockito with spring boot list is not available

hi every one i have some problem
i have a test class called TestUserService
#RunWith(MockitoJUnitRunner.class)
public class TestUserService {
#InjectMocks
UserService userService;
#Mock
InMemory inMemory;
#Before
public void init(){
MockitoAnnotations.initMocks(this);
}
#Test
public void registry(){
System.out.println("hi");
UserDto userDto=new UserDto(1L,"abed","alrhman",26);
UserDto userDto1=new UserDto(1L,"abdallah","almasre",27);
UserDto userDto2=new UserDto(1L,"bisher","alahmad",35);
UserDto userDto3=new UserDto(1L,"ibrahem","alrabe",32);
userService.registry(userDto);
userService.registry(userDto1);
userService.registry(userDto2);
userService.registry(userDto3);
System.out.println("\"registry\" = " + "registry");
verify(inMemory,times(0)).save(userDto.ToDTO(userDto));
verify(inMemory,times(1)).save(userDto1.ToDTO(userDto1));
verify(inMemory,times(0)).save(userDto2.ToDTO(userDto2));
verify(inMemory,times(0)).save(userDto3.ToDTO(userDto3));
}
}
when run the test i want save new user by UserService class and UserService will go to InMemory class
#Service
public class UserService implements IUserService {
private final InMemory inMemory;
public UserService(InMemory inMemory){
this.inMemory=inMemory;
}
#Override
public UserDto registry(UserDto userDto) {
User user=userDto.ToDTO(userDto);
User user1=inMemory.save(user);
return user.toEntity(user1);
}
}
the problem in InMemory Class it is a local repository has Map Called users and some other methods
after called registry(User user) method
the Map has it when debugger 'this' is not available
and the user param not receive into the save(User user) method and the debugger dont walk of any point check
private Map<Long,User> users=new HashMap<>();
#Override
public User save(User user) {
user.setId(1L);
users.put(1L,user);
return user;
}
and this is Image when debugger code
if you attention save(User user) dont has object or any value and users map not avalible
In your test, you are defining InMemory as a mock. A mock is an object that emulates the behavior of a real object according to how you configure such mock to behave. Having said that, your InMemory object in your test is not a real InMemory object, but a simple mock which means that its real code will not be called. Take a look at #Spy and consider replacing #Mock with it. Find more information in the following articles:
https://medium.com/swlh/what-is-the-difference-between-a-mock-and-a-stub-bd6b639e9fa5
https://www.baeldung.com/mockito-annotations#mock-annotation
in first step we must replace #Mock to Spy
and then we must change in when() method to when(spyInstance.method) and then we need called method has spyInstance.method to testing
this is my code after fixed you can see it
#Spy
InMemory inMemoryGet; // replace it from mock to spy
#InjectMocks
UserService userService;
#Before
public void init(){
MockitoAnnotations.initMocks(this);
}
#Test
public void registry(){
UserDto emp = new UserDto(1L,"Lokesh","Gupta",25); // add it
when(inMemoryGet.save(emp.ToDTO(emp))).thenReturn(emp.ToDTO(emp));
verify(inMemoryGet, times(0)).save(emp.ToDTO(emp)); // how many time invoke
userService.registry(emp); // called method from here
verify(inMemoryGet, times(1)).save(emp.ToDTO(emp));
System.out.println("\"Test\" = " + "Pass");
}

Spring Kafka ProducerRecord Mock Test

ProducerRecord<Key,Value> producerRecord = new ProducerRecord<>(topicName, key,value);
ListenableFuture<SendResult<Key,Value>> future = kafkaTemplate.send(producerRecord);
Test Case
#Mock
ProducerRecord<Key,Value> producerRecord;
#Mock
ListenableFuture<SendResult<Key,Value>> future;
#Mock
private KafkaTemplate<Key,Value> producer;
When am mocking send method it will return null future object
when(producer.send(producerRecord)).thenReturn(future);
Solution I Found.
I Pass any(ProducerRecord.class) so i will trigger your method and return you future object
#Mock
ListenableFuture<SendResult<Key,Value>> future;
#Mock
private KafkaTemplate<Key,Value> producer;
when(producer.send(any(ProducerRecord.class))).thenReturn(future);
Besides mocking the KafkaTemplate#send method:
when(producer.send(producerRecord)).thenReturn(future);
You should instruct your class fixture implementation to initialize the mock objects. It should be a pre-instance step as follows:
#BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
}
It is common good practice to move mock configuration (common ones) to the set-up step as well:
#BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
SendResult<Key,Value> result = Mockito.mock(SendResult.class);
when(future.get()).thenReturn(result);
when(producer.send(producerRecord)).thenReturn(future);
}

Mockito test coming back as zero

I am trying to get call method called totalmoney() to get the total money in the h2 database but it always returns 0.
#RunWith(MockitoJUnitRunner.class)
public class MoneyTests {
#InjectMocks
MoneyServiceImplementation MoneyServiceImplementation;
#Before
public void init() {
MockitoAnnotations.initMocks(this);
}
#Test
public void getAllMoney() {
long total_money = MoneyServiceImplementation.TotalMoney();
assertEquals("2000", total_money);
}}
But it will return the right amount of 2000 by:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
MoneyService MoneyService = (MoneyService) context.getBean("MoneyServiceImplementation");
long total_money = MoneyService.TotalMoney();
So what am i doing wrong in the test that it will not work?
Mockito is not an dependency injection framework, don't expect this shorthand utility to inject a complex graph of objectsbe it mocks/spies or real objects.
Again, note that #InjectMocks will only inject mocks/spies created using the #Spy or #Mock annotation.
There are no any #Mock or #Spy annotated beans in your test, so all of the dependencies in the created MoneyServiceImplementation are null.

How to test DaoImpl methods using Junit and Mockito

I am using spring with jdbcTemplate for my app and I want to test DaoImpl class. There is implementation for insertion, updation and retrieval operation
Dao class Method
//dummy class
public class PlayerDAOImpl implements PlayerDAO {
#Autowired
private JdbcTemplate jdbcTemplate;
public Integer getPlayer(int playerId) {
String sql = "SELECT ccount(1) FROM PLAYER WHERE
PLAYER_ID = ?";
return (jdbcTemplate. queryForObject("Query", new Object[]{playerId},
Integer.class)!=0); //here only throws exception
}
//other methods
}
and for that I have written Test class which execute successfully for insertion and updation but while retrieving it is giving nullpointer exception.
#RunWith(MockitoJUnitRunner.class)
class Test{
#InjectMocks
PlayerDAOImpl dao;
#Mock
JdbcTemplate jdbcTemplate;
#Test
public void retrieveResult(){
Mockito.when(dao.getPlayer(int playerId)).thenReturn(false);
//Assert Statement
}}
I have googled/tried but not found solution which worked for me. So how to test that method or inject jdbcTemplate so that it will succeed.
Thanks for Help!!
The problem is that you are trying to mock the class under test (PlayerDAOImpl) instead of its dependency (JdbcTemplate).
Change you mock to something like:
Mockito.when(jdbcTemplate.queryForObject(Mockito.any(), Mockito.any(), Mockito.any()).thenReturn(COUNT);
Where COUNT is an Integer, and then write your assertions on the return value of dao.getPlayer.

Mockito test case for repository layer EntityManager chained methods

Repository code to be tested using Mockito:
public List<X> findAll() {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<X> criteriaQuery = criteriaBuilder.createQuery(X.class);
Root<X> policyRoot = criteriaQuery.from(X.class);
return entityManager.createQuery(criteriaQuery.select(policyRoot)).getResultList();
}
How to mock cretedQuery() and getResultList() methods of entityManger?
#Test
public void testCase() {
when(entityManager.getCriteriaBuilder()).thenReturn(new
CriteriaBuilderImpl(any()));
Query query = new QueryImpl(any(), any(), any());
when(entityManager.createQuery(anyString())).thenReturn(query);
when(query.getResultList()).thenReturn(attributeList);
}
It throws NullPointerException.
Use mocks. Make sure the entityManager is injectable in the class you try to test. For a unit test I would definitely do a test like this. It does not test the database, but it does test all the calls that are done and the general application logic. I agree you should also write an integration test that will test the 'real' database call and result.
#RunWith(MockitoJUnitRunner.class)
public class QueryTest {
#Mock
TypedQuery<X> query;
#Mock
CriteriaBuilderImpl criteriaBuilder;
#Mock
CriteriaQuery<X> criteriaQuery;
#Mock
Root<X> policyRoot;
#Mock
EntityManager manager; // This mock should be injected in the class that is been tested
#InjectMocks
TestClass sut; //System Under Test
#Test
public void test() {
when(manager.getCriteriaBuilder()).thenReturn(criteriaBuilder);
when(criteriaBuilder.createQuery(any(X.class)).thenReturn(criteriaQuery);
when(criteriaQuery.from(any(X.class)).thenReturn(policyRoot);
when(criteriaQuery.select(eq(policyRoot))).thenReturn(criteriaQuery);
when(manager.createQuery(eq(criteriaQuery)).thenReturn(query);
when(query.getResultList()).thenReturn(Collections.emptyList());
List<X> result = sut.findAll();
// now verify
verify(manager, times(1)).getCriteriaBuilder();
verify(criteriaBuilder, times(1)).createQuery(any(X.class));
// and so on
// now write your assertions
assertEquals(0, result.getSize());
}
}

Resources