在DispatcherServlet中找不到带有URI [/ oauth / token]的HTTP请求的映射,其名称为''

时间:2016-02-02 14:27:55

标签: spring spring-security spring-boot

当我尝试在spring-boot环境中运行测试时,我收到此错误。

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

但如果我设置本地服务器并通过邮递员向服务器发送请求,一切都很好。

班级配置:

@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
    @Transactional
    public void driverAuthorized() throws Exception {
        String accessToken = getAccessToken("user", "pass");

        mvc.perform(get("/xxx").
            header("Authorization", "Bearer" + accessToken))
            .andExpect(status().isOk());
    }
}

0 个答案:

没有答案