I am getting this error, when I am trying to run Test in spring-boot environment.
2016-02-02 14:52:37.770 WARN 23136 --- [ main] o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/oauth/token] in DispatcherServlet with name ''
java.lang.AssertionError: Status
Expected :200
Actual :404
but if I set up local server and send request to server by postman everything is fine.
Class configuration:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
public class DriverResourceIntTest{
// [...]
@Autowired
FilterChainProxy springSecurityFilterChain;
private MockMvc mvc;
private User user;
@PostConstruct
public void setup() {
MockitoAnnotations.initMocks(this);
UserResource userResource = new UserResource();
ReflectionTestUtils.setField(userResource, "userService", userService);
ReflectionTestUtils.setField(userResource, "userMapper", userMapper);
this.mvc = MockMvcBuilders.standaloneSetup(userResource)
.setCustomArgumentResolvers(pageableArgumentResolver)
.setMessageConverters(jacksonMessageConverter)
.addFilter(springSecurityFilterChain)
.build();
}
//gettingAccessTokenTestMethod
private String getAccessToken(String username, String password) throws Exception {
String authorization = "Basic "
+ new String(Base64Utils.encode("user:uSecret".getBytes()));
String content = mvc;
.perform(post("/oauth/token")
.secure(true)
.header("Authorization", authorization)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("username", username)
.param("password", password)
.param("grant_type", "password")
.param("scope", "read write")
.param("client_id", "user")
.param("client_secret", "uSecret"))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
return content.substring(17, 53);
}
// Test in which I get this error:
@Test
@Transactional
public void driverAuthorized() throws Exception {
String accessToken = getAccessToken("user", "pass");
mvc.perform(get("/xxx").
header("Authorization", "Bearer" + accessToken))
.andExpect(status().isOk());
}
}