gpt4 book ai didi

java - 无法在测试行为调用的 Java 函数中使用 Citrus 变量

转载 作者:行者123 更新时间:2023-11-30 01:55:44 25 4
gpt4 key购买 nike

我想在测试中循环以实现一种行为,该行为将使用从“消息模板(文件)”构建的消息响应某些请求,其中我将一些字符串替换为柑橘测试变量的值和索引环形。我想我几乎成功了,但不幸的是,当我尝试在我的行为中使用 ReplaceAll String 函数时,我的测试崩溃了。请参阅下面我编写的代码片段,其中删除了所有不必要的部分,以便希望使我的问题易于理解

   public class myBehavior extends AbstractTestBehavior {

private String payloadData;

myBehavior withPayloadData(String payload) {
this.payloadData = payload;
return this;
}

@Override
public void apply() {
echo("[behavior] - OK ->behavior is invoked");
echo("[behavior]" + payloadData + " - OK ->variable from Test is correctly transmitted to behavior");
echo(func_asis(payloadData));
echo(func_replace(payloadData)); // if you uncomment this line the test will crash at starting time when invoking replace_all
}


String func_asis(String myvar)
{
String s = "This is a string in which nothing is replaced, OK fine !";
echo("[func_asis] OK ->in func_asis now ");
echo("[func_asis] myvar="+ myvar + " - OK ->variable from Test is correctly transmitted to func_asis");
return s;
}

String func_replace(String myvar)
{
String s = "This is a string in which to replace !!Name!! by the value of my citrus variable but it crashes";
echo("[func_replace] OK ->in func_replace");
echo("[func_replace] myvar="+ myvar + " - OK ->variable from Test is correctly transmitted to func_asis");
//s=s.replaceAll("!!Name!!",myvar); // This will crash when starting the test (not actually when running it) !!!
return s;
}

}

@CitrusTest
public void mySimpleTest() throws IOException {
description("Simple Test invoking a behavior which it self will invoke a java function");
variable("vm", "/dc/vm/folder/vm_basename");

repeat().until("i = 3")
.actions(
sleep(1000L),
applyBehavior(new myBehavior().withPayloadData("${vm}${i}"))
);
}
  • 以下是在 func_replace 中注释了 ReplaceAll 调用的测试输出。
    09:21:22,102 DEBUG port.LoggingReporter| BEFORE TEST SUITE
09:21:22,102 INFO port.LoggingReporter|
09:21:22,103 INFO port.LoggingReporter|
09:21:22,103 INFO port.LoggingReporter| BEFORE TEST SUITE: SUCCESS
09:21:22,103 INFO port.LoggingReporter| ------------------------------------------------------------------------
09:21:22,103 INFO port.LoggingReporter|
09:21:22,119 DEBUG t.TestContextFactory| Created new test context - using global variables: '{}'
09:21:22,128 INFO port.LoggingReporter|
09:21:22,128 INFO port.LoggingReporter| ------------------------------------------------------------------------
09:21:22,128 DEBUG port.LoggingReporter| STARTING TEST CitrusLearning.mySimpleTest <com.grge.citrus.cmptest.stratus>
09:21:22,128 INFO port.LoggingReporter|
09:21:22,129 DEBUG citrus.TestCase| Initializing test case
09:21:22,130 DEBUG context.TestContext| Setting variable: citrus.test.name with value: 'CitrusLearning.mySimpleTest'
09:21:22,130 DEBUG context.TestContext| Setting variable: citrus.test.package with value: 'com.grge.citrus.cmptest.stratus'
09:21:22,130 DEBUG context.TestContext| Setting variable: vm with value: '/dc/vm/folder/vm_basename'
09:21:22,130 DEBUG citrus.TestCase| Test variables:
09:21:22,131 DEBUG citrus.TestCase| citrus.test.name = CitrusLearning.mySimpleTest
09:21:22,131 DEBUG citrus.TestCase| citrus.test.package = com.grge.citrus.cmptest.stratus
09:21:22,131 DEBUG citrus.TestCase| vm = /dc/vm/folder/vm_basename
09:21:22,131 INFO port.LoggingReporter|
09:21:22,131 DEBUG port.LoggingReporter| TEST STEP 1/1: repeat
09:21:22,131 DEBUG port.LoggingReporter| TEST ACTION CONTAINER with 9 embedded actions
09:21:22,131 DEBUG context.TestContext| Setting variable: i with value: '1'
09:21:22,134 INFO actions.SleepAction| Sleeping 1000 ms
09:21:23,139 INFO actions.SleepAction| Returning after 1000 ms
09:21:23,139 INFO actions.EchoAction| [behavior] - OK ->behavior is invoked
09:21:23,139 INFO actions.EchoAction| [behavior]/dc/vm/folder/vm_basename1 - OK ->variable from Test is correctly transmitted to behavior
09:21:23,140 INFO actions.EchoAction| [func_asis] OK ->in func_asis now
09:21:23,140 INFO actions.EchoAction| [func_asis] myvar=/dc/vm/folder/vm_basename1 - OK ->variable from Test is correctly transmitted to func_asis
09:21:23,140 INFO actions.EchoAction| This is a string in which nothing is replaced, OK fine !
09:21:23,140 INFO actions.EchoAction| [func_replace] OK ->in func_replace
09:21:23,141 INFO actions.EchoAction| [func_replace] myvar=/dc/vm/folder/vm_basename1 - OK ->variable from Test is correctly transmitted to func_asis
09:21:23,141 INFO actions.EchoAction| This is a string in which to replace !!Name!! by the value of my citrus variable but it crashes
09:21:23,143 DEBUG leanExpressionParser| Boolean expression 2 = 3 evaluates to false
09:21:23,143 DEBUG context.TestContext| Setting variable: i with value: '2'
09:21:23,143 INFO actions.SleepAction| Sleeping 1000 ms
09:21:24,145 INFO actions.SleepAction| Returning after 1000 ms
09:21:24,145 INFO actions.EchoAction| [behavior] - OK ->behavior is invoked
09:21:24,145 INFO actions.EchoAction| [behavior]/dc/vm/folder/vm_basename2 - OK ->variable from Test is correctly transmitted to behavior
09:21:24,145 INFO actions.EchoAction| [func_asis] OK ->in func_asis now
09:21:24,145 INFO actions.EchoAction| [func_asis] myvar=/dc/vm/folder/vm_basename2 - OK ->variable from Test is correctly transmitted to func_asis
09:21:24,145 INFO actions.EchoAction| This is a string in which nothing is replaced, OK fine !
09:21:24,146 INFO actions.EchoAction| [func_replace] OK ->in func_replace
09:21:24,146 INFO actions.EchoAction| [func_replace] myvar=/dc/vm/folder/vm_basename2 - OK ->variable from Test is correctly transmitted to func_asis
09:21:24,146 INFO actions.EchoAction| This is a string in which to replace !!Name!! by the value of my citrus variable but it crashes
09:21:24,146 DEBUG leanExpressionParser| Boolean expression 3 = 3 evaluates to true
09:21:24,146 INFO port.LoggingReporter|
09:21:24,147 DEBUG port.LoggingReporter| TEST STEP 1/1 SUCCESS
  • 这是取消注释该行时的崩溃日志。
        09:27:51,525 DEBUG port.LoggingReporter| BEFORE TEST SUITE
09:27:51,525 INFO port.LoggingReporter|
09:27:51,525 INFO port.LoggingReporter|
09:27:51,525 INFO port.LoggingReporter| BEFORE TEST SUITE: SUCCESS
09:27:51,525 INFO port.LoggingReporter| ------------------------------------------------------------------------
09:27:51,525 INFO port.LoggingReporter|
09:27:51,542 DEBUG t.TestContextFactory| Created new test context - using global variables: '{}'
09:27:51,551 INFO port.LoggingReporter|
09:27:51,552 ERROR port.LoggingReporter| TEST FAILED CitrusLearning.mySimpleTest <com.grge.citrus.cmptest.stratus> Nested exception is:
java.lang.IllegalArgumentException: No group with name {vm}
at java.util.regex.Matcher.appendReplacement(Matcher.java:849)
at java.util.regex.Matcher.replaceAll(Matcher.java:955)
at java.lang.String.replaceAll(String.java:2223)
at com.grge.citrus.cmptest.stratus.CitrusLearning$myBehavior.func_replace(CitrusLearning.java:180)
at com.grge.citrus.cmptest.stratus.CitrusLearning$myBehavior.apply(CitrusLearning.java:163)
at com.consol.citrus.dsl.design.AbstractTestBehavior.apply(AbstractTestBehavior.java:53)
at com.consol.citrus.dsl.design.ApplyTestBehaviorAction.doExecute(ApplyTestBehaviorAction.java:38)
at com.consol.citrus.actions.AbstractTestAction.execute(AbstractTestAction.java:42)
at com.consol.citrus.dsl.design.DefaultTestDesigner.applyBehavior(DefaultTestDesigner.java:193)
at com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner.applyBehavior(TestNGCitrusTestDesigner.java:168)
at com.grge.citrus.cmptest.stratus.CitrusLearning.mySimpleTest(CitrusLearning.java:194)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:216)
at com.consol.citrus.testng.AbstractTestNGCitrusTest.invokeTestMethod(AbstractTestNGCitrusTest.java:121)
at com.consol.citrus.dsl.testng.TestNGCitrusTest.invokeTestMethod(TestNGCitrusTest.java:121)
at com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner.invokeTestMethod(TestNGCitrusTestDesigner.java:73)
at com.consol.citrus.dsl.testng.TestNGCitrusTest.run(TestNGCitrusTest.java:110)
at com.consol.citrus.dsl.testng.TestNGCitrusTest.run(TestNGCitrusTest.java:56)
at org.testng.internal.MethodInvocationHelper.invokeHookable(MethodInvocationHelper.java:242)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:579)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:135)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeSingleClass(TestNGDirectoryTestSuite.java:112)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:99)
at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:146)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)
at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407)
09:27:51,555 INFO port.LoggingReporter| ------------------------------------------------------------------------
09:27:51,555 INFO port.LoggingReporter|
09:27:51,595 INFO port.LoggingReporter|
09:27:51,595 INFO port.LoggingReporter| ------------------------------------------------------------------------
09:27:51,595 DEBUG port.LoggingReporter| AFTER TEST SUITE

非常感谢任何解决此问题的想法

最佳答案

投入更多时间后,我开始了解 Citrus 如何处理变量,在上面的情况下,“myvar”的值实际上设置为“${vm}${i}”,并将在执行时被替换时间,但是当从测试操作调用时......所以我查看了自定义测试操作,发现即使有时变量实例化也有点棘手......但尽管如此,我仍然可以实现我想做的第一部分就是用变量内容替换字符串中的一些预定义值。请参阅下面执行此操作的代码片段。

...但是现在因为我从操作中调用替换函数,所以我无法将字符串返回给调用者,而这正是我计划在行为中执行的操作...因此将调查是否以及如何可能或将看看我的测试的一些其他设计(比如存储字符串的临时文件,其中替换值在 func_replace 更新它们之后由行为读取。)无论如何,这是我上面遇到的问题的解决方案:


@Test
public class CitrusLearningL4 extends TestNGCitrusTestDesigner {

public class myBehavior extends AbstractTestBehavior {

private @CitrusResource TestContext parentContext;

myBehavior withContext(@Optional @CitrusResource TestContext context) {
this.parentContext=context;
return this;
}

@Override
public void apply() {
echo("[behavior] - OK ->behavior is invoked");
func_replace();
echo("[behavior] - OK ->behavior is finished");

}

void func_replace()
{
final String s = "This is a string in which '!!Name!!' is present because it was replaced by the value of my test variable";
echo("[func_replace] OK ->in func_replace");
action(new AbstractTestAction() {
public void doExecute(TestContext context) {
String s1 = s;
System.out.println("[anAction] - OK ->anAction is invoked");
String sVar=String.format("%s", (String) parentContext.getVariable("vm") + (String) parentContext.getVariable("i"));
System.out.println("[anAction] - OK ->" + s1.replaceAll("!!Name!!",sVar));
}
});
}

}

@Test @Parameters("context")
@CitrusTest
public void mySimpleBehaviorTest(@Optional @CitrusResource TestContext context) throws IOException {
description("Simple Test invoking a behavior which it self will invoke a java function");
variable("vm", "/dc/vm/folder/vm_basename");

repeat().until("i = 3")
.actions(
sleep(1000L),
applyBehavior(new myBehavior().withContext(context))
);
}

}

关于java - 无法在测试行为调用的 Java 函数中使用 Citrus 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54604556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com