Created
September 23, 2014 20:08
-
-
Save apetro/c096e64b638e5d00e1f0 to your computer and use it in GitHub Desktop.
Trivial JUnit test for a Spring Portlet MVC controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.wisc.my.portlet.identityLinking.mvc.portlet; | |
import edu.wisc.my.portlet.identityLinking.model.NoPersonForEmplIdException; | |
import edu.wisc.my.portlet.identityLinking.model.Person; | |
import edu.wisc.my.portlet.identityLinking.service.IdentityLinkingService; | |
import org.junit.Test; | |
import org.springframework.web.portlet.ModelAndView; | |
import javax.portlet.PortletRequest; | |
import javax.portlet.RenderRequest; | |
import javax.portlet.RenderResponse; | |
import static org.junit.Assert.assertEquals; | |
import static org.mockito.Mockito.mock; | |
import static org.mockito.Mockito.when; | |
/** | |
* Unit tests for main controller. | |
*/ | |
public class MainControllerTest { | |
/** | |
* The showMainView method routes to the main view. | |
*/ | |
@Test | |
public void showMainViewRoutesToMainView() { | |
final MainController controller = new MainController(); | |
final IdentityLinkingService linkingService = mock(IdentityLinkingService.class); | |
controller.setIdentityLinkingService(linkingService); | |
final RenderRequest portletRequest = mock(RenderRequest.class); | |
final RenderResponse portletResponse = mock(RenderResponse.class); | |
final ModelAndView routedView = controller.showMainView(portletRequest, portletResponse); | |
assertEquals("main", routedView.getViewName()); | |
} | |
/** | |
* When the emplId is unrecognized, the controller routes to the user not found view, | |
* including the not-found emplId in the model. | |
* | |
* @throws NoPersonForEmplIdException should not throw, that would always be a test failure. | |
*/ | |
@Test | |
public void lookupUserRoutesNotFoundViewWhenEmplIdUnrecognized() | |
throws NoPersonForEmplIdException { | |
// never actually throws | |
final MainController controller = new MainController(); | |
final IdentityLinkingService linkingService = mock(IdentityLinkingService.class); | |
controller.setIdentityLinkingService(linkingService); | |
final NoPersonForEmplIdException notFoundException = new NoPersonForEmplIdException("Fuller"); | |
when(linkingService.lookupPersonByEmplId("Fuller")).thenThrow(notFoundException); | |
ModelAndView modelAndView = controller.lookupUser("Fuller"); // as in, Buckminster | |
assertEquals("noPersonWithEmplId", modelAndView.getViewName()); | |
assertEquals("Fuller", modelAndView.getModel().get("emplId")); | |
} | |
/** | |
* When the emplId is recognized, the controller routes to the user found view, | |
* including the found user in the model. | |
*/ | |
@Test | |
public void lookupUserRoutesFoundViewWithPersonWhenFound() | |
throws NoPersonForEmplIdException { | |
final MainController controller = new MainController(); | |
final IdentityLinkingService linkingService = mock(IdentityLinkingService.class); | |
controller.setIdentityLinkingService(linkingService); | |
final Person vanHise = mock(Person.class); | |
when(linkingService.lookupPersonByEmplId("vanhise")).thenReturn(vanHise); | |
final ModelAndView modelAndView = controller.lookupUser("vanhise"); | |
assertEquals("foundPerson", modelAndView.getViewName()); | |
assertEquals(vanHise, modelAndView.getModel().get("foundPerson")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment