0
ヘッダーのベアラー値のために、別のクラスユーザーのSecurityITest.loginで正常にログインして提供されたトークンにアクセスする必要があります。それは(クラス内または外)その他の試験によってアクセスできるように、ログインによって生成されたトークンを保存するのに最適な場所ですログインテストのデータ結果を他のテストに使用する
BaseITest
@AutoConfigureMockMvc
@SpringBootTest(classes = Application.class)
public class BaseITest extends AbstractTestNGSpringContextTests {
@Autowired
protected MockMvc mvc;
@Autowired
ObjectMapper mapper;
}
SecurityIITest
public class SecurityIITest extends BaseITest {
@Value("${bootstrap.username}")
private String username;
@Value("${bootstrap.password}")
private String password;
@BeforeSuite(groups = {"security"})
public void login() throws Exception {
String jsonResult = mvc.perform(post(ApiUrls.LOGIN)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("username", username)
.param("password", password))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath(JsonField.TOKEN).exists())
.andReturn().getResponse().getContentAsString();
JsonNode result = mapper.readTree(jsonResult);
// this token to reuse in other methods from other class
// token = result.get("token").asText();
}
}
AccountControllerITest
public class AccountControllerITest extends BaseITest {
@Test(dependsOnGroups = {"security"})
public void postAccount() throws Exception {
// need to access token here
}
}