title: Spring MVC test controller with spring security
body: <p>I am trying to write the test case for test controller, here is the code for the controller</p>
  
  <pre><code>@Controller
  @RequestMapping("/")
  @SessionAttributes({"roles", "departments"})
  public class AppController {
  
      @Autowired
      UserService userService;
  
      @Autowired
      RoleService roleService;
  
      @Autowired
      DepartmentService departmentService;
  
      @Autowired
      MessageSource messageSource;
  
      @Autowired
      PersistentTokenBasedRememberMeServices persistentTokenBasedRememberMeServices;
  
      @Autowired
      AuthenticationTrustResolver authenticationTrustResolver;
  
      static final Logger logger = LoggerFactory.getLogger(AppController.class);
  
      /**
       * This method will list all existing users.
       */
      @RequestMapping(value = { "/", "/list" }, method = RequestMethod.GET)
      public String listUsers(ModelMap model) {
  
          List&lt;User&gt; users = userService.findAllUsers();
          model.addAttribute("users", users);
          model.addAttribute("loggedinuser", getPrincipal());
          return "userslist";
      }
      /**
       * This method returns the principal[user-name] of logged-in user.
       */
      private String getPrincipal(){
          String userName = null;
          Object principal = getCurrentUser();
  
          if (principal instanceof UserDetails) {
              userName = ((UserDetails)principal).getUsername();
          } else {
              userName = principal.toString();
          }
          return userName;
      }
  
      private Object getCurrentUser(){
          return SecurityContextHolder.getContext().getAuthentication().getPrincipal();
      }
  //The rest part of the controller}
  </code></pre>
  
  <p>I using TestNG based on the this tutorial: <a href="http://websystique.com/springmvc/spring-4-mvc-and-hibernate4-integration-testing-example-using-annotations/" rel="nofollow">http://websystique.com/springmvc/spring-4-mvc-and-hibernate4-integration-testing-example-using-annotations/</a>, and currently I have following in my test cases:</p>
  
  <pre><code>//all the import file
  public class AppControllerTest {
  
      @Mock
      UserService userService;
  
      @Mock
      MessageSource message;
  
      @InjectMocks
      AppController appController;
  
      @Spy
      List&lt;User&gt; users = new ArrayList&lt;User&gt;();
  
      @Spy
      ModelMap model;
  
      @Mock
      BindingResult result;
  
      @BeforeClass
      public void setUp(){
          MockitoAnnotations.initMocks(this);
          users = getUsers();
      }
  
      private List&lt;User&gt; getUsers() {
          // TODO Auto-generated method stub
          User u1 = new User();
          u1.setId(1);
          u1.setFirstName("Admin");
          u1.setLastName("Admin");
          u1.setUsername("admin");
          u1.setEmail("admin@akb.co.jp");
          u1.setDateOfBirth(new LocalDate());
          u1.setPassword("admin");
          Department admin = new Department();
          admin.setId(1);
          admin.setName("Admin");
          admin.setDescription("Admin");
          u1.setDepartment(admin);
          Role adminRole = new Role();
          adminRole.setId(1);
          adminRole.setRoleName("ADMIN");
          Set&lt;Role&gt; roles = new HashSet&lt;&gt;();
          roles.add(adminRole);
          u1.setRoles(roles);
  
          User u2 = new User();
          u2.setId(1);
          u2.setFirstName("Alice");
          u2.setLastName("Lin");
          u2.setUsername("alice.lin");
          u2.setEmail("alice.lin@akb.co.jp");
          u2.setDateOfBirth(new LocalDate());
          u2.setPassword("Alice0102");
          u2.setDepartment(admin);
          u2.setRoles(roles);
  
          users.add(u1);
          users.add(u2);
          return users;
      }
  
      @Test
      public void listUsers(){
          when(userService.findAllUsers()).thenReturn(users);
          Assert.assertEquals(appController.listUsers(model), "userslist");
          Assert.assertEquals(model.get("users"), users);
          verify(userService, atLeastOnce()).findAllUsers();
      }
  }
  </code></pre>
  
  <p>Now the question is, if I didn't comment this line <code>model.addAttribute("loggedinuser", getPrincipal());</code>
  in my controller class, when I run maven test, it will throw null pointer exception, that is obvious, since in my test cases I didn't login to the application. What can I do so I can make the test passed include this line?</p>