Hi all,
I am wirting junit test case. I used mokito to set the current session even i set current session i am getting null pointer exception in below line.
Page.getCurrent().setTitle(“My Project”);
Below code is juint test case:
public class UserListViewTest extends AbstractComponent{
private static final long serialVersionUID = 1L;
UI userListViewTest;
UserListView userListView;
ViewChangeEvent event;
VaadinSession session;
public static VaadinSession session1;
UserListTable userListTable;
VaadinService vaadinService;
MyVaadinServlet mymockServlet;
MyVaadinServletService mockService;
ServletConfig mockServletConfig;
HttpSession mockHttpSession;
WrappedSession mockWrappedSession;
Lock httpSessionLock;
VaadinRequest request;
VaadinResponse response;
User user;
private JPAContainer<User> container;
public static UI ui,uI;
@SuppressWarnings("deprecation")
@Before
public void setUp() throws Exception {
//Mockito.when(UI.getCurrent()).thenReturn(Mockito.mock(UI.class));
request=org.mockito.Mockito.mock(VaadinRequest.class);
response=org.mockito.Mockito.mock(VaadinResponse.class);
vaadinService=org.mockito.Mockito.mock(VaadinService.class);
vaadinService.setCurrentInstances(request, response);
vaadinService.setCurrent(vaadinService);
session = org.mockito.Mockito.mock(VaadinSession.class);
session1=org.powermock.api.mockito.PowerMockito.mock(VaadinSession.class);
PowerMockito.mockStatic(UI.class);
ui=org.powermock.api.mockito.PowerMockito.mock(UI.class);
uI=org.powermock.api.mockito.PowerMockito.mock(UI.class);
httpSessionLock = new ReentrantLock();
mockServletConfig = new MockServletConfig();
mymockServlet = new MyVaadinServlet();
mymockServlet.init(mockServletConfig);
mockService = mymockServlet.getService();
mockHttpSession = Mockito.mock(HttpSession.class);
mockWrappedSession = new WrappedHttpSession(mockHttpSession) {
ReentrantLock lock = new ReentrantLock();
{
lock.lock();
}
@Override
public Object getAttribute(String name) {
Object res;
try {
Thread.sleep(100); // for deadlock testing
org.junit.Assert.assertTrue("Deadlock detected", httpSessionLock.tryLock(5, TimeUnit.SECONDS)); // simulates
String lockAttribute = mockService.getServiceName() + ".lock";
if (lockAttribute.equals(name)) {
res = lock;
} else if ("com.vaadin.server.VaadinSession.Mock Servlet".equals(name)) {
res = session;
} else {
res = super.getAttribute(name);
}
httpSessionLock.unlock();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return res;
}
};
session = new VaadinSession(mockService);
user= new User();
user.setuserId(4l);
user.setRole("ADMIN");
user.setUsername("admin");
user.setPassword("admin");
System.out.println("mockService = " + mockService);
System.out.println("mockWrappedSession = " + mockWrappedSession);
System.out.println("session = " + session);
session.storeInSession(mockService, mockWrappedSession);
session.setCurrent(session);
session.setAttribute("loginurl", "loginurl");
String loginurl = "www.google.com";
session.getCurrent().setAttribute("userobj", user);
}
@After
public void tearDown() throws Exception {
}
@Test
public void testEnter() {
userListTable=new UserListTable();
PersistenceUtil.createEntityManager();
container = JPAContainerFactory.make(User.class, PersistenceUtil.getEntityManager());
userListTable.createTable();
//page=org.mockito.Mockito.mock(Page.class);
event=org.mockito.Mockito.mock(ViewChangeEvent.class);
//setCurrent(ui.getSession());
ui.setSession(org.powermock.api.mockito.PowerMockito.mock(VaadinSession.class));
ui.setCurrent(org.powermock.api.mockito.PowerMockito.mock(UI.class));
//ui.setSession(ui.getSession());
ui.getCurrent().setSession(org.powermock.api.mockito.PowerMockito.mock(VaadinSession.class));
//ui.setCurrent(s);
UI.setCurrent(ui);
//ui.setParent(uI);
System.out.println("Ui get current "+ ui.getCurrent());
System.out.println("Ui get session "+ui.getSession());
System.out.println("Ui get page "+ui.getPage());
System.out.println("Page current "+Page.getCurrent());
userListView = new UserListView();
userListView.enter(event);
}
}
i this what im getting in console
Ui get current uI
Ui get session null
Ui get page null
Page current null
session.getSession = vaadin sessioncom.com.mypoject.views.ListViewTest$1@1387e9b2
Please give some suggestion to me to clear this error Thanks a lot in advance.