Java 尝试从junit访问worklight.properties时出现Null异常
使用MobileFirst平台6.3 当我尝试使用junit从worklight.properties获取worklight项目中服务器文件夹的配置路径时,返回的值为null 我正在使用以下代码获取路径Java 尝试从junit访问worklight.properties时出现Null异常,java,junit,ibm-mobilefirst,mobilefirst-adapters,mobilefirst-server,Java,Junit,Ibm Mobilefirst,Mobilefirst Adapters,Mobilefirst Server,使用MobileFirst平台6.3 当我尝试使用junit从worklight.properties获取worklight项目中服务器文件夹的配置路径时,返回的值为null 我正在使用以下代码获取路径 WorklightConfiguration.getInstance().getString("mbaas.configRootDir"); 编辑:这就是我想做的。我在junit中运行这段代码,它应该返回true public class Test2 { @Test publ
WorklightConfiguration.getInstance().getString("mbaas.configRootDir");
编辑:这就是我想做的。我在junit中运行这段代码,它应该返回true
public class Test2 {
@Test
public void test() {
//customProperty=123
String str=getWorklightProperty("customProperty");
assertEquals("123", str);
}
public String getWorklightProperty(String propertyName) {
return WorklightConfiguration.getInstance().getString(propertyName);
}
}
编辑:这对JUnit不起作用。运行此代码时,它应连接到Worklight服务器 当您通过调用适配器来运行它时,it适配器正在与服务器对话,这就是您可以得到响应的原因 当您直接运行它时,它没有可与之通信的服务器,这就是为什么会出现
null
您需要验证您的代码是否有效。
我已在MFP 6.3中测试了以下内容,并成功地从worklight.properties检索到该值:
customProperty=123
package com.myClass.customcode;
import com.worklight.server.bundle.api.WorklightConfiguration;
public class Test {
public static String getWorklightProperty(String propertyName){
return WorklightConfiguration.getInstance().getString(propertyName);
}
}
function test() {
return {
result: com.myClass.customcode.Test.getWorklightProperty("customProperty")
}
}
{
"isSuccessful": true,
"result": "123"
}
您正在尝试对通常在Worklight Server环境中运行的某些代码进行单元测试,具体取决于
WorklightConfiguration.getInstance().getString(propertyName);
这只能在服务器内部运行,而不能在JUnit之类的独立单元测试下运行
如何解决这个问题?首先,你到底想测试什么?您真的在尝试测试WorklightConfiguration.getInstance().getString()是否有效?你为什么要做这样的事?您是否建议测试每个Worklight API?我主张您应该对代码进行单元测试,而不是Worklight。因此,如果您有这样的伪代码:
figure out the name of a property
WorklightConfiguration.getInstance().getString(thePropertyWeJustFigured)
do some stuff with the value we obtained
然后,您可以通过提供WorklightConfiguration API的模拟实现对代码进行单元测试。您可以使用JMock之类的框架,这样您的代码就可以在JUnit中执行,而不依赖Worklight。这是真正的单元测试,没有依赖性的测试
就我个人而言,我不太赞成这种方法,因为准备模拟的工作量很大。相反,我更喜欢做结构化集成测试。也就是说,当适配器在Worklight服务器内部运行时,我将它作为一个整体进行测试。我可能仍然使用JUnit,但它运行的测试使用HTTP调用框架来调用适配器。因此,测试脚本如下所示:
ensure worklight server is running and adapter under test is deployed
run JUnit tests that issue HTTP requests to the adapter and inspect the results.
您正在使用哪个版本的worklight?您所指的“配置”路径是什么?Worklight项目中的服务器文件夹下没有此类文件夹、文件或属性;请你的问题更准确一些。详细说明您试图实现的场景。WorklightConfiguration.getInstance().getString(“mbaas.configRootDir”);这是它的确切外观,在server/conf文件夹下的work light.properties文件中定义。mbaas.configRootDir=../worklightproject/server/conf这是mbaas.configRootDir在work light中的外观。properties@RahulChadalawada,谢谢你的澄清。约尔的问题的答案是什么?您的Worklight版本是什么?6.0/6.1/6.2/6.3?问题是Idan 1)正如我提到的,我正在通过junit运行它,所以我的要求是通过junit运行它。2)它通过适配器运行良好,但当我说通过junit运行时,它抛出空值。我已经根据我在问题中的要求编辑了您给出的代码示例。请检查,谢谢,我知道我为什么这么做了吗只有在通过junit运行时才能获取null异常。请提供帮助。您好,Idan,是否有其他方法可以从junit获取worklight.properties值。