I need to run a JUnit test on the below code:
Asked
Active
Viewed 98 times
1 Answers
1
I'll work through one of your methods, the "happy-path" approach. It's up to you to test alternate examples, i.e. if ready is false and so on, but the basic structure is arrange, execute, verify, though since you're using mockito and multiple mocks and no test subject, I've added an initiate step just so you can see the additional mock you need to support your step, plus the test subject.
@Test
public void testdoGet() {
//Initiate
ReadyCheck readyCheck = new ReadyCheck();
Writer writer = mock(Writer.class); //not sure which writer it is off the top of my head
//Arange
when(response.getWriter()).thenReturn(writer); //we pass the writer on the get
when(response.getStatus()).thenReturn(HttpServletResponse.SC_OK); //we pass a status
//Execute
readyCheck.doGet(request, response);
//Verify
verify(response, times(1)).setStatus(HttpServletResponse.SC_OK);
verify(writer, times(1)).write("Ready!");
}

Compass
- 5,867
- 4
- 30
- 42
-
Isn't this already a duplicate of http://stackoverflow.com/questions/5434419/how-to-test-my-servlet-using-junit – Vishal Jumani Jul 13 '16 at 15:17
-
@VishalJumani I guess, I don't know dupes off the top of my head for all JUnits – Compass Jul 13 '16 at 15:19
-
-