GridworksServlet is now unit tested with a single regression test

git-svn-id: http://google-refine.googlecode.com/svn/trunk@765 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Iain Sproat 2010-05-14 14:57:04 +00:00
parent 2703457863
commit 0af4e294f8
2 changed files with 116 additions and 0 deletions

View File

@ -0,0 +1,39 @@
package com.metaweb.gridworks.tests;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.metaweb.gridworks.GridworksServlet;
import com.metaweb.gridworks.commands.Command;
/**
* Exposes protected methods of com.metaweb.gridworks.GridworksServlet as public for unit testing
*
*/
public class GridworksServletStub extends GridworksServlet{
//requirement of extending HttpServlet, not required for testing
private static final long serialVersionUID = 1L;
/**
* Helper method for inserting a mock object
* @param command
*/
static public void InsertTestCommand( Command command ){
_commands.put("test-command", command);
}
/**
* Helper method for clearing up after testing
*/
static public void RemoveTestCommand( ){
_commands.remove("test-command");
}
public void wrapDoGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
super.doGet(request, response);
}
}

View File

@ -0,0 +1,77 @@
package com.metaweb.gridworks.tests;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.metaweb.gridworks.commands.Command;
public class GridworksServletTests {
// logging
final static protected Logger logger = LoggerFactory.getLogger("CancelProcessesCommandTests");
//System under test
GridworksServletStub SUT = null;
//mocks
HttpServletRequest request = null;
HttpServletResponse response = null;
Command command = null;
@Before
public void SetUp()
{
SUT = new GridworksServletStub();
request = mock(HttpServletRequest.class);
response = mock(HttpServletResponse.class);
command = mock(Command.class);
GridworksServletStub.InsertTestCommand(command); //inject mock into command container
}
@After
public void TearDown(){
SUT = null;
request = null;
response = null;
command = null;
GridworksServletStub.RemoveTestCommand(); //remove mock to clean command container
}
//--------------------doGet tests---------------------
@Test
public void doGetRegressionTest(){
when(request.getPathInfo()).thenReturn("/test-command/foobar"); //called in getCommandName method
try {
SUT.wrapDoGet(request, response);
} catch (ServletException e) {
Assert.fail();
} catch (IOException e) {
Assert.fail();
}
verify(request,times(1)).getPathInfo();
try {
verify(command,times(1)).doGet(request, response);
} catch (ServletException e) {
Assert.fail();
} catch (IOException e) {
Assert.fail();
}
}
}