我已经从教程中撤出了集成测试,以测试通过OAuth2(以Google作为授权主机)保护的API。我正在使用MockMVC对象并通过webApplication上下文设置webAppContext,但是当我运行它时,我立即在webApplicationContext上收到了一个IllegalArgument异常。
我搜索了StackOverflow,发现出现了很多问题,但是没有一个答案可以解决我的问题。大多数建议建议添加@RunWith和@WebAppConfiguration批注,我已经完成了。但是它仍然无法正常工作。协助将不胜感激。
package xyz;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.OAuth2Request;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
public class TempTest4 {
@Autowired private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Test
public void testGetAuthenticationInfo() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext)
.apply(springSecurity())
.build();
mockMvc.perform(MockMvcRequestBuilders.get("/api/token")
.with(authentication(getOauthTestAuthentication()))
.sessionAttr("scopedTarget.oauth2ClientContext", getOauth2ClientContext()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.username").value("bwatkins"))
.andExpect(jsonPath("$.token").value("my-fun-token"));
}
private Authentication getOauthTestAuthentication() {
return new OAuth2Authentication(getOauth2Request(), getAuthentication());
}
private OAuth2Request getOauth2Request () {
String clientId = "oauth-client-id";
Map<String, String> requestParameters = Collections.emptyMap();
boolean approved = true;
String redirectUrl = "http://my-redirect-url.com";
Set<String> responseTypes = Collections.emptySet();
Set<String> scopes = Collections.emptySet();
Set<String> resourceIds = Collections.emptySet();
Map<String, Serializable> extensionProperties = Collections.emptyMap();
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("Everything");
OAuth2Request oAuth2Request = new OAuth2Request(requestParameters, clientId, authorities,
approved, scopes, resourceIds, redirectUrl, responseTypes, extensionProperties);
return oAuth2Request;
}
private Authentication getAuthentication() {
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("Everything");
User userPrincipal = new User("user", "", true, true, true, true, authorities);
HashMap<String, String> details = new HashMap<String, String>();
details.put("user_name", "bwatkins");
details.put("email", "bwatkins@test.org");
details.put("name", "Brian Watkins");
TestingAuthenticationToken token = new TestingAuthenticationToken(userPrincipal, null, authorities);
token.setAuthenticated(true);
token.setDetails(details);
return token;
}
private OAuth2ClientContext getOauth2ClientContext () {
OAuth2ClientContext mockClient = mock(OAuth2ClientContext.class);
when(mockClient.getAccessToken()).thenReturn(new DefaultOAuth2AccessToken("my-fun-token"));
return mockClient;
}
}