Apex 错误:“保存错误:方法不存在或签名不正确”
我目前正在学习 apex(使用 Force.com IDE),在为自定义控制器编写测试时遇到了一些麻烦。
控制器类如下:
public with sharing class CustomController {
private List<TestObject__c> objects;
public CustomController() {
objects = [SELECT id, name FROM TestObject__c];
}
public List<TestObject__c> getObjects() {
return objects;
}
}
测试类为:
@isTest
private class ControllerTest {
static testMethod void customControllerTest() {
CustomController controller = new CustomController();
System.assertNotEquals(controller, null);
List<TestObject__c> objects;
objects = controller.getObjects();
System.assertNotEquals(objects, null);
}
}
在 objects =controller.getObjects();
行上,我收到一条错误消息:
保存错误:方法不存在或签名不正确:[CustomController].getObjects()
有人知道我为什么会收到此错误吗?
I'm currently learning apex (using the Force.com IDE), and I'm running into some trouble when writing a test for a custom controller.
The controller class is as follows:
public with sharing class CustomController {
private List<TestObject__c> objects;
public CustomController() {
objects = [SELECT id, name FROM TestObject__c];
}
public List<TestObject__c> getObjects() {
return objects;
}
}
and the test class is:
@isTest
private class ControllerTest {
static testMethod void customControllerTest() {
CustomController controller = new CustomController();
System.assertNotEquals(controller, null);
List<TestObject__c> objects;
objects = controller.getObjects();
System.assertNotEquals(objects, null);
}
}
On the objects = controller.getObjects();
line I'm getting an error which says:
Save error: Method does not exist or incorrect signature: [CustomController].getObjects()
Anyone have an idea as to why I'm getting this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个很好的简写:
它为你创建了 getter/setter,并且在我看来看起来更干净。至于您当前的问题,是的 - 很难将代码直接保存到生产中 - 特别是在单独的文件中测试类。
最好在沙箱/开发组织中执行此操作,然后部署到生产环境(部署到服务器 - Force.com IDE)。但如果您必须直接保存到生产中,那么我会将测试方法与您的类结合起来。但从长远来看,在专用测试类之上使用 @test 是正确的选择。这样就不会消耗您宝贵的资源。
A nice shorthand:
It creates the getter/setter for you and looks cleaner imo. As for your current issue, yes - it's hard saving code directly into production - especially with test classes in separate files.
Best to do this in a sandbox/dev org then deploy to production (deploy to server - Force.com IDE). But if you must save directly into production then I'd combine test methods with your class. But in the long run, having @test atop a dedicated test class is the way to go. It won't consume your valuable resources this way.