2017-09-21 7 views
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 

    } 
} 

答えて

1

テストは同じ<test>タグ内にある場合、あなたは、によって、複数渡ってそれの一部である@Test方法でデータを共有することができます。データを取得するには、データ

Object object = new Object(); 
Reporter.getCurrentTestResult().getTestContext().setAttribute("foo", object); 

を設定するには

Object obj = Reporter.getCurrentTestResult().getTestContext().getAttribute("foo"); 

から@Test方法。

テストは<test>別のタグの中にありますが、同じ<suite>以内に、あなたは、データ

Object obj = Reporter.getCurrentTestResult().getTestContext().getSuite().getAttribute("foo"); 
を取得するには、データ

Object object = new Object(); 
Reporter.getCurrentTestResult().getTestContext().getSuite().setAttribute("foo", object); 

を設定するには

を起動してデータを共有することができた場合

内から@Test方法。

関連する問題