gpt4 book ai didi

java - 将使用静态方法的测试类变成Spring Boot服务?

转载 作者:行者123 更新时间:2023-12-01 23:32:48 24 4
gpt4 key购买 nike

我使用 Drools 作为消息系统中验证机制的 validator 。

我想将本地测试控制台应用程序的一部分转换为服务。使用 Springboot + Thymleaf 与其他类一切正常,但我想验证来自公开服务的一些消息。

问题是我使用了我在测试类中继承的父类(super class)(DroolsTest)。

将其变成 Controller 似乎不起作用。

我在构造函数部分遇到空指针异常。

   super("myParameter",RULE_FILES);

我如何将其转换为 Controller ?这可能真的很容易,但我真的很困惑。

谢谢各位提前的 friend 。

此代码段适用于本地。

public class messageToTest_Test **extends DroolsTest** {

public static String[] RULE_FILES;

static {
RefDataXmlPath ref = new RefDataXmlPath();
ref.updateXmlPathbaseOnPhase(Phase.myParameter, Iteration.myParameter);

GetInformationFromXmlMapping getRulesFromXmlMapping = new GetInformationFromXmlMapping();
String rules[] = getRulesFromXmlMapping.getRulesByMessages(Iteration.myParameter, Phase.myParameter, "messageToTest").toArray(new String[0]);
RULE_FILES = rules;
}

public messageToTest_Test() throws IOException {
super("myParameter",RULE_FILES);


}

@Test
public void messageToTest_Test_1() throws Exception {
SOME CODE HERE.....
}
}

DroolsTest 类中的构造函数

public DroolsTest( String iteration, String... ruleFiles) {
this.RULES_PATH = "Rules/" + iteration + "/";
this.ruleFiles = ruleFiles;
this.MESSAGES_PATH = "src/test/resources/XML_Messages/" + iteration + "/";
}

最佳答案

I get Nullpointer exceptions for the constructor part .

只能在当前类的构造函数中调用继承类的构造函数。 super("myParameter",RULE_FILES) 会初始化一个类,但它不像方法中那样工作,因为只有在类已经初始化时才能调用方法本身。 (我不明白你的 IDE 如何不将其标记为无效语法。)

如果您需要在测试开始之前设置一些属性并且您可以访问这些字段,请使用 @Before, @BeforeTest @BeforeAll etc.

public class MessageToTest_Test **extends DroolsTest** {

public static String[] RULE_FILES;

@BeforeClass
public static void beforeClass() {
RefDataXmlPath ref = new RefDataXmlPath();
ref.updateXmlPathbaseOnPhase(Phase.myParameter, Iteration.myParameter);

GetInformationFromXmlMapping getRulesFromXmlMapping = new GetInformationFromXmlMapping();
String rules[] = getRulesFromXmlMapping.getRulesByMessages(Iteration.myParameter, Phase.myParameter, "messageToTest").toArray(new String[0]);
super.ruleFiles = rules;
...
}

@Before
public void beforeEachTest () {
/*.. other things ..*/
}

public messageToTest_Test() throws IOException {
/*... test ...*/

}
}

如果您没有访问权限,请使用默认构造函数进行设置:

public class MessageToTest_Test **extends DroolsTest** {

public static String[] RULE_FILES;

static {
RefDataXmlPath ref = new RefDataXmlPath();
ref.updateXmlPathbaseOnPhase(Phase.myParameter, Iteration.myParameter);

GetInformationFromXmlMapping getRulesFromXmlMapping = new GetInformationFromXmlMapping();
String rules[] = getRulesFromXmlMapping.getRulesByMessages(Iteration.myParameter, Phase.myParameter, "messageToTest").toArray(new String[0]);
RULE_FILES = rules;
}

MessageToTest_Test () {
super("myParameter",RULE_FILES);
}
...
}

How could i convert this into a Controller.?

不太明白你的意思。如果您想单独测试设置 - 将它们提取到一个单独的类中,并将其作为可以在构造函数中传递的组件进行测试。

关于java - 将使用静态方法的测试类变成Spring Boot服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58284762/

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