Refresh Your Java - Before Java Interview
We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....
Junit has some limitaions , for instance we could not test
the Private and Static methods
with JUNIT, this issue is
addressed by some other Unit Testing APIs such EasyMock,
Mockito
and all.
Here is the sample Mocktio Unit Test case.
Step 1 :
Util
class which has a Static Method.
class TestUtil
{
public static printName
(Srting name ) {
System.out.println("Your Name is
" + name);
}
}
Step 2
:
Your service that to be
tested.
class YourService {
public YourService() {
}
public void printName (Srting Name) {
TestUtil.printName(Name);
}
}
Step 3:
Your Test
Case
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import
static org.powermock.api.mockito.PowerMockito.doNothing;
import static
org.powermock.api.mockito.PowerMockito.whenNew;
import org.apache.tools.ant.taskdefs.Length.When;
import org.junit.Before;
import
org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import
org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ TestUtil.class})
public
class TestBroadBandService {
@Before
public void createData()
{
PowerMockito.mockStatic(TestUtil.class);
when (TestUtil.printName("")
.thenReturn("newName");
}
@Test
public
void testInvokeWebService() {
YourService YourService = new
YourService();
yourService.printName("YourName");
}
}
This Portal is intended to put all Java/J2ee related topics at one single place for quick referance, not only Technical , but also the Project Management Related thing such as Development Process methodoogies build process, unit testing etc.,
This Portal has More than 500 Java Interview Questions (also could be Considered as Quick Notes) very neatly separated topic by topic with simple diagrams which makes you easily understandable. Importantly these are from our Realtime expericance.