You need to sign in to do that
Don't have an account?
AAlex11
Test txt file from static resource
Hi,
I wrote a controller extension with a class which read txt file from static resource inside.
The class in my controller is like this:
I tried to test the class, as usual, creating test data for static resource but I've realize that DML operations aren't allowed on StaticResource object.
Anyone could help me?
Thanks
I wrote a controller extension with a class which read txt file from static resource inside.
The class in my controller is like this:
public String textFromSomeUniqueName { get { StaticResource sr = [ select Body from StaticResource where Name = 'SomeUniqueName' ]; return sr.Body.toString(); } }Now I have to write test class for my controller extension but I don't know how testing this particular part.
I tried to test the class, as usual, creating test data for static resource but I've realize that DML operations aren't allowed on StaticResource object.
Anyone could help me?
Thanks
Replace your get method with below code -
return (Test.isRunningTest() ? ' test data ': [SELECT Body FROM StaticResource WHERE Name = 'Cond01' ].Body.toString());
It will minimise the statement. Even after this your code is not covered then please clear earlier test run result and rerun the test class.
Thanks,
Ishwar Shinde
All Answers
You can't create static resorce in test class, but you can simply tweak the code to work for test method. In get method check
if(Test.isRunningTest()){
retrun ' test data ';
}else{
StaticResource sr = [
select Body
from StaticResource
where Name = 'SomeUniqueName'
];
return sr.Body.toString();
}
Accordingly write your test method.
Thanks,
Ishwar
I'll post my controller e my test class
The test passes, but there's no coverage for the string get method.
if you can tell me where am I wrong please.
thanks
Replace your get method with below code -
return (Test.isRunningTest() ? ' test data ': [SELECT Body FROM StaticResource WHERE Name = 'Cond01' ].Body.toString());
It will minimise the statement. Even after this your code is not covered then please clear earlier test run result and rerun the test class.
Thanks,
Ishwar Shinde