0

I want to mock a static method taking two parameters, HttpServletRequest and Principal.

//Here is the method signature
public static String getRole(HttpServletRequest request, Principal principal)  throws Exception {
}

I am defining the security test config for injecting security credentials into the test class.

// security test config
@Configuration
@EnableWebSecurity
@EnableWebMvc
static class Config extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws    Exception {
         auth.inMemoryAuthentication().withUser("user").password("password")
        .roles("USER");
    }
}

I am writing the junit test case for a method that internally calls the above static method. How to write the expectation for the above static method in the junit class?

when(StaticClassA.getRole(??????, ??????)).thenReturn("ROLE1");
Mordechai
  • 15,437
  • 2
  • 41
  • 82
Lalitha
  • 1
  • 1
  • Don't use html tags, SO defines its own formatting tools. – Mordechai Dec 14 '16 at 23:06
  • 1
    Possible duplicate of [Mocking static methods with Mockito](http://stackoverflow.com/questions/21105403/mocking-static-methods-with-mockito) – David Rawson Dec 15 '16 at 02:40
  • David, Its not a duplicate. I did go through the link. I am testing a controller method which takes HttpServletRequest and Principal as the arguments. This method in turn calls a static method by passing these arguments. Is it possible to write expectation for this static call in Mockito/PowerMock? – Lalitha Dec 15 '16 at 18:27
  • have you tried 'doReturn().when()' syntax instead of 'when().thenReturn()' ? – arghtype Dec 16 '16 at 11:20

0 Answers0