Monday, October 25, 2021
What is the difference between date and instant?
Sunday, October 24, 2021
Monday, October 18, 2021
Projection in hibernate
Projection are used to execute aggregate operations and for single value query .
EX -
public static void main(String[] args) {
SessionFactory factory = new Configuration().configure().addAnnotatedClass(Student.class).buildSessionFactory();
Session session = factory.getCurrentSession();
try {
session.beginTransaction();
Criteria c = session.createCriteria(Student.class);
Projection p = Projections.property("lastName");
List<String> students = c.setProjection(p).list();
for(String s:students)
System.out.println(s);
session.getTransaction().commit();
session.close();
} finally {
factory.close();
}
}
It will return values of last name column.
Projections are useful for
1.Fetching only certain column of the table.
2.To execute aggregate function.
Monday, October 11, 2021
@Mock,@InjectMocks and @RunWith(MockitoJunitRunner.class) in mockito
In previous example we have created a Mock like below-
DataService dataServiceMock = mock(DataService.class);
but we can create mock using @Mock annotations.
@Mock
DataService dataServiceMock;
when mock is created then we need to inject into beans
@InjectMocks
SomeBusinessImpl businessImpl;// dataServiceMock will inject into SomeBusinessImpl
Then we no need to inject this dependencies through constructor or setter injection.
MockitoJUnitRunner checks if all settings are ok or any mistakes have take places.
so put it to the top of the test class
@RunWith(MockitoJUnitRunner.class)
Friday, October 1, 2021
Previous example using mockito
package com.example.mockitoEx.MockitoEx;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.Test;
class SomeBusinessTestMock {
@Test
public void findGreatestFromAllData() {
DataService dataServiceMock = mock(DataService.class);
when(dataServiceMock.retrieveAllData()).thenReturn(new int[] { 24, 15, 10 });
SomeBusinessImpl businessImpl = new SomeBusinessImpl(dataServiceMock);
int result = businessImpl.findTheGreatestFromAllData();
assertEquals(24, result);
}
}
Using mock method we can create a mock object of any interface or object.
Fluent interface pattern
public class UserConfigurationManager { private String userName; private String password; private UserConfigurationManager() { ...
-
If there is multiple implementation of an interface then we need to specify which one's implementation will use. For that reason we need...
-
public class MainApp { public static void main(String[] args) { String input = "aabbbcccdeefghijkkkkkk"; calculateFrequen...
-
If any event fired on a DOM element it will also fired on all of its parent element which is called the event bubbling. So, the element eve...