如何在柑橘框架中重用情景?

时间:2019-04-03 12:57:34

标签: continuous-integration refactoring integration-testing code-reuse citrus-framework

我使用citrus框架设置了这个测试场景。现在,我正在尝试在其他情况下重用它。 我正在为每个步骤创建行为。我的行为主要是http请求

public class NoProductDocumentValidationScenarioIT {

    private @CitrusResource TestContext parentContext;

    @CitrusEndpoint(name = "todoBasicAuthClient")
    private HttpClient cmsAuthClient;

    @CitrusEndpoint(name = "vdmBasicAuthClient")
    private HttpClient vdmAuthClient;

    @CitrusEndpoint(name = "gvHttpClient")
    private HttpClient gvHttpClient;

    @Test
    @CitrusTest
    public String NoProductDocumentValidation(@CitrusResource TestRunner runner, @CitrusResource TestContext context)
            throws BadNewsMLG2Exception {
        String pdtIdentifier = "EDIT-FR-SVID2-YM9N001479";
        String videoDocument = VideoDocument.setUpVideoDocument("fr", "v1_afptv_sport_broadcast_photos");
        int jobPublicationID = 5;
        // CMS Authentification
        TestBehavior authenticateCMS = new ProductAuthenticationBehavior(cmsAuthClient);
        ApplyTestBehaviorAction authenticateActionCMS = new ApplyTestBehaviorAction(runner, authenticateCMS);
        authenticateActionCMS.doExecute(context);

        // Document Creation
        CreateVideoDocumentBehavior createDoc = new CreateVideoDocumentBehavior(cmsAuthClient, pdtIdentifier,
                videoDocument);
        ApplyTestBehaviorAction createDocAction = new ApplyTestBehaviorAction(runner, createDoc);
        createDocAction.doExecute(context);

        // get document data
        videoDocument = createDoc.getVideoDocument();
        G2VideoDocument g2VideoDocument = ((G2VideoDocument) G2ObjectFactory.parse(videoDocument));
        g2VideoDocument.getProducts();
        String linkToVideoDocument = g2VideoDocument.getLinkToSelf().toString();
        String linkToProject = g2VideoDocument.getLinkToVideoProject().toString();
        String projectID = IrisStringTools.extractIdFromUri(linkToProject);
        String documentID = IrisStringTools.extractIdFromUri(linkToVideoDocument);
        String etag = g2VideoDocument.getEditorialTag();

        // Lock document Metadata
        EditVideoDocumentMetaBehavior lockDocMeta = new EditVideoDocumentMetaBehavior(cmsAuthClient, pdtIdentifier,
                videoDocument, documentID);
        ApplyTestBehaviorAction lockDocMetaAction = new ApplyTestBehaviorAction(runner, lockDocMeta);
        lockDocMetaAction.doExecute(context);
}
}

我在Eclipse中运行它作为JUnit测试。

我考虑过使用超类,但是没有用。

public class ProductDocumentValidationScenarioIT extends NoProductDocumentValidationScenarioIT {

    public String ProductDocumentValidation(@CitrusResource TestRunner runner, @CitrusResource TestContext context)
            throws BadNewsMLG2Exception {
                return something;

    }
}

2 个答案:

答案 0 :(得分:0)

测试行为是解决问题的方法。我建议使用这样的东西

CreateVideoDocumentBehavior createDoc = new CreateVideoDocumentBehavior(cmsAuthClient, pdtIdentifier,
                videoDocument);

runner.applyBehavior(createDoc);

答案 1 :(得分:0)

我们最后要做的是创建一个实例化所有行为的行为运行程序(java类),然后在该场景中,我们将行为运行程序与行为常数对应于我所需的行为:

public class BehaviorRunner {
private void doExecute(TestRunner runner, TestContext context, TestBehavior testBehavior) {
    ApplyTestBehaviorAction behaviorAction = new ApplyTestBehaviorAction(runner,testBehavior);
    behaviorAction.doExecute(context);
}

public void execute( String behaviorLabel, @CitrusResource TestRunner runner, @CitrusResource TestContext context) {
    try {
        switch (behaviorLabel) {
            case BehaviorConstants.CREATE_VIDEO_DOCUMENT :
                CreateVideoDocumentBehavior createVideoDocumentBehavior = new CreateVideoDocumentBehavior(cmsAuthClient, pdtIdentifier, VideoDocument.setUpVideoDocument2(LanguageConstants.EN, "v1_afptv_sport_broadcast_photos"));
                doExecute(runner, context, createVideoDocumentBehavior);
                break;
            case BehaviorConstants.MOVIEDRAFT :
                MovieDraftDocumentBehavior movieDraftDocumentBehavior = new MovieDraftDocumentBehavior(cmsAuthClient, pdtIdentifier, 1, g2VideoDoc);
                doExecute(runner, context, movieDraftDocumentBehavior);     
                break;
            case BehaviorConstants.PUBLICATION_PROGRESSION_STATUS:
                GetPublicationProgressionStatusBehavior publicationProgressionStatusBehavior = new GetPublicationProgressionStatusBehavior(vdmAuthClient, pdtIdentifier , g2VideoDoc);
                doExecute(runner, context, publicationProgressionStatusBehavior);   
                break;
            case BehaviorConstants.VALIDATE :
                ValidateDocumentBehavior validateDocumentBehavior = new ValidateDocumentBehavior(cmsAuthClient, pdtIdentifier, g2VideoDoc);
                doExecute(runner, context, validateDocumentBehavior);   
                break;
            default:
                    break;
        }   

我们最终遇到了这样的情况:

@Test
@CitrusTest
public void NoProductDocumentValidation(@CitrusResource TestRunner runner, @CitrusResource TestContext context) throws BadNewsMLG2Exception {
    slf4jLogger.info("Montage analysis scenario START");
    // execute scenario
    // Document Creation 
    behaviorRunner.execute(BehaviorConstants.CREATE_VIDEO_DOCUMENT, runner, context);
    // Lock document Metadata
    behaviorRunner.execute(BehaviorConstants.EDITOR, runner, context);
    // Lock Document Binary 
    behaviorRunner.execute(BehaviorConstants.BINARY_EDITOR, runner, context);

因为我们在不同情况下使用了不同的行为组合,所以节省了很多代码行。

我希望这会对某人有所帮助!