以下是我的代码,其中@Autowired
字段位于其中:
种皮
@ContextConfiguration("classpath:spring.xml")
public abstract class TestA extends AbstractTestNGSpringContextTests {
@Autowired
@Value("#{environment.username}")
public String username;
@Autowired
@Value("#{environment.password}")
public String password;
@Autowired
@Value("#{environment.passwordtoken}")
public String passwordToken;
}
这是我尝试使用上述字符串变量的另一个类
public class TestData extends TestA {
ForceApi api;
public void setApi() {
// Instantiating api by setting username and pwd
System.out.println("In getApi");
ApiConfig ac = new ApiConfig();
ac.setUsername(username);
ac.setPassword(passwordToken); ac.setLoginEndpoint("https://login.salesforce.com/services/Soap/u/34.0/");
api = new ForceApi(ac);
}
public void pTestData() {
setApi();
// Create test data for play group admin
String idSoql = "SELECT id FROM PlayGroup__c";
List<Map> mapResults = api.query(idSoql).getRecords();
}
}
尝试使用以下类执行测试:
public class TestB extends TestA{
@Test(dataProvider="browsers")
public void test1(){
TestData td = new TestData();
td.pTestData();
}
}
请帮我确定一下我做错了什么。我在这一行得到Null Pointer异常:
List<Map> mapResults = api.query(idSoql).getRecords();
答案 0 :(得分:1)
我自己解决了上述问题。
我在使用@Autowired字段的类中使用了@Service(“testdata”)
@Service("testdata")
public class TestData extends TestA {
ForceApi api;
public void setApi() {
// Instantiating api by setting username and pwd
System.out.println("In getApi");
ApiConfig ac = new ApiConfig();
ac.setUsername(username);
ac.setPassword(passwordToken); ac.setLoginEndpoint("https://login.salesforce.com/services/Soap/u/34.0/");
api = new ForceApi(ac);
}
public void pTestData() {
setApi();
// Create test data for play group admin
String idSoql = "SELECT id FROM PlayGroup__c";
List<Map> mapResults = api.query(idSoql).getRecords();
}
}
并在TestB中更改如下
public class TestB extends TestA{
@Test(dataProvider="browsers")
public void test1(){
TestData td = (TestData )applicationContext.getBean("testdata");
td.pTestData();
}
}
并在spring.xml中添加了bean,如下所示
<bean id="testdata" class="data.TestData"/>
用Null指针Exception解决了我的问题。