You need to sign in to do that
Don't have an account?
Developer.mikie.Apex.Student
Help with test code for extension and question about extensions
Hey, was wondering if I could get a hand on writing a test for my extension below V. I also had a question: This extension is for the save button and re-directs to my VF page which a tabbed account view. I will have to write several extensions for each of the custom object tabs...is it possible to write one super extension which will re-direct all of them upon saving to my VF page?
public class extSaveButton { public Fact_Finder__c fac {get;set;} public extSaveButton(ApexPages.StandardController controller) { this.fac = (Fact_Finder__C)controller.getRecord(); } Public PageReference saveFactFinder(){ insert fac; // Send the user to the detail page for the new account. PageReference pageRef= new PageReference('/apex/DestinyAccountTest?id='+fac.account__c+'&Sfdc.override=1'); pageRef.getParameters().put('tab','FactFinder'); return pageRef; } }
if it it is simple extension I usually create a simple testMethod at the end of the class. If the extension is being used in different class, then I usually would just create one big class testMethod that runs hits all classes and extensions.
To help with your extension test here is a simple testMethod you can use:
Hope this helps.
All Answers
if it it is simple extension I usually create a simple testMethod at the end of the class. If the extension is being used in different class, then I usually would just create one big class testMethod that runs hits all classes and extensions.
To help with your extension test here is a simple testMethod you can use:
Hope this helps.
I have tried using test code...but when I tried to make a class with the code that you psoted...I got an error:
Error: Compile Error: Test methods must be in test classes at line 19 column 28
What I mean is...say I have many custom objects which re-direct to new record VF pages. Upon clicking saving, do I have to write an extension for every object so that it will re-direct to the open tab or can I use the one I just posted as an extension for all my new record pages and simply add to them?
So when doing unit testing on the codes, you want to cover to try to reach to 100% code coverage in your class. How are you using the redirect in other VF pages? Are you just referring it based on one extension or are you creating multiple extensions with the same method? If you are reusing this extension then you would only need to cover code coverage for this extension. An example would be you have 5 different VF pages that have the same extension, therefore you would only have 1 extension and would only cover that class. But if you are using multiple extension with similar methods, then you would have to cover those code coverage for each class extension. For example, if you have 3 different VF pages, and 3 different extension with the same method of redirecting, then you would have to run a test coverage for all those classess separatey. I hope this make sense.
Basically I just copy and pasted your code into a new apex class and then I changed all the c's -> f's, all the cons to facs and all the contact into fact_finder__c.
The way it works is I have my main account VF tabbed page, the detail page is the information on the account and then each other tab is set up as a custom or standard object that is a child to account with apageblocktable of the records with commandlinks re-rendering the bottom half of each tab for the records. There is a new record button that I have placed above the pageblocks which re-directs to a custom VF new record page...This new record page has the extsavebutton as an extension....it re-directs as shown above using the VF page url and the account id gotten through the custom object it is creating the new record for and the rest of the URL which is about overiding I beleive... I hope this makes sense?
Thank you so much for your time eric.
Still getting the error....I wonder why its not allowing me to just add it to the extension...Test methods msut be in test classes..what does that mean?
I wrote this...but I am pretty sure that I did it wrong..
@isTest
private class TestExtSaveButton {
static testMethod void TestExtSaveButton(){
Fact_Finder__c facTest = new Fact_Finder__c(Account__c = 'TestExtension');
Apexpages.Standardcontroller stdFac = new Apexpages.Standardcontroller(facTest);
extSaveButton ext = new extSaveButton(stdFac);
ext.saveFactFinder();
}
}
All right i managed to recreate your whole structure in my environment and this should work. So in your current extension append all that stuff.
Also, please take a look at this on how to write a good unit test:
http://wiki.developerforce.com/page/How_to_Write_Good_Unit_Tests
Let me know if the code works out for you.
Figured it out, Thank you sooo much for your help.
This was the end code, essentially your code but changed to be in its own class.
So, will I have to create a separate extension for every new record re-direction or can I add to the extensiona bove and use it for a few (if they are all re-directing to the same page?
I gave your thing a try and it still says that test methods must be in test classes...I read something a few minutes ago about a change in a recent update that makes it so that test classes must be in their own class....could this be why, or have you used it like this recently?
Check this out: Could this be right...
http://salesforce.stackexchange.com/questions/12516/spring-13-test-methods-must-be-in-test-classes-error
Yea I'm using this now and the code is sitting on my dev winter 14 sandbox. Not sure why it;s not working but glad you were able to figure it out.
And to answer your question, you can have 1 class to house all your testMethod and they can be almost the same format except changing the Custom Object. The reason for this is because you are creating multiple extension classes and you need to basically run a test scenario to hit those classes. So essentially your class method will look somehting like this:
New code after reviewing your suggestions. This way I w8ill not have to change the ID to one from my production when porting it over. Thank you mate, you have really helped me. This is final test class:
@isTest
private class TestExtSaveButton {
static testMethod void TestExtSaveButton(){
//Based on your code and what you give me, Fact Finder is a child of the Account and when you //attach fact finder it needs to be reference into an account.
// create a test account that will be use to reference the Fact Finder Record
Account acc = new Account(Name = 'Test Account');
insert acc;
//Now lets create Fact Finder record that will be reference for the Standard Account
Fact_Finder__c facTest = new Fact_Finder__c(Account__c = acc.id);
//call the apepages stad controller
Apexpages.Standardcontroller stdFac = new Apexpages.Standardcontroller(facTest);
//now call the class and reference the standardcontroller in the class.
extSaveButton ext = new extSaveButton(stdFac);
//call the pageReference in the class.
ext.saveFactFinder();
}
}
So you can make one super test method for all the Testclasses...except you cant make one super extension which will house all the different savebutton extensions? Is that right?
Awesome bud... and you are right my stupid api was runnin on version 26 therefore it didn't error out. Okay i will put a big NOTE to myself next time when writing class to just use a seperate class.
And as for hard coding Ids, never like the idea and when i started with SFDC and testing i used to hard code a lot of values in sandbox and when i pushed it out to prod everything breaks. So now i never hardcode anything and when I do hard code i use custom labels and custom settings.
Glad i can help out.
Yes, you can create one big TestClass but you cant create one test for all your extension unless you create a dynamic visualforce page and controller. That is another topic that you might want to look into, but it takes a lot of work and understanding all the dynamic controller stuff.
k, thank you for your time Eric. I really appreciate it you legend.
So sorry to bother you again buddy...but I was wondering if you could give me a hand with writing a test for my attachmentAccount extension...I can only get 52%. Don't feel obligated, only if you have time.
Test:
Extension:
Thank you in advance
You almost got it try this:
It still failed. but we are up to 69% coverage. These were the errors:
(Is it telling me that I need to change something inside the actual class (twice) and once inside the test class?
Error Message System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name, Body]: [Name, Body] Stack Trace Class.AccountExtension.upload: line 33, column 1
Class.AccountExtension.save: line 25, column 1
Class.TestAccountExtension.TestAccountExtension: line 22, column 1
You know what i just realized, I can't seem to understand what you are trying to do in the code using the attachment. I should have read the code thoroughly.
Do you think you can paste VF page where you upload the attachment?
So i have to tell you how much i hate writing unit test for Attachments. Anyway's based on your code I kind of tried to tweak it, so here it
I was able to run this code and test it and i got 100%. So hopefully this helps.
I really appreciate your help then. When i tried to change the extension to the new one on the VF page it said:
Error: Unknown property 'AccountStandardController.attach'
What could that mean. The reference for attach is in the new extension aswell isnt it?
Here is the VF code, sorry for the late reply..only just read that bit.
It is a tab in an accounts VF page where the user can save an attachment and then the reads the tab paramaters and reloads to the same attachments tab. I hope this helps.
Your code tested 100%, except my VF page will not accept it because of an unknown property accountstandardcontroller.attach. Basically, the controller originally was there as a way in which a user would attach an attachment to an account. The functionality was then added to re-direct the user back to the VF page (after saving), then further functionality was added to re-direct to the attachments tab....Something weird just happened, I have done it a thousand times and a thousand times it had worked, but randomly when attempting to save an attachment today....it re-directed to the details tab and the attachments tab was no longer visible.....quite wierd, what do you think might be the reason?
Sorry, how embarassing, I changed the pageref to one of my other VF pages which is identical asides from the attachments tab. But for some reason the page will still not recognize property 'AccountStandardController.attach'.
Got it buddy, for some reason the new extension would not add to the VF page..it was not recognizing the attach variable for some reason...Either way, the original test code that you helped me fix up worked like a charm..I just had to add this line to my extension:
attach.Name = 'Unit Test Attachment';
attach.Body = Blob.valueOf('Unit Test Attachment Body');
Thank you so much for your time and help. Such a legend.
your welcome buddy...
Ended up using the second code and test afterall. When I added those lines to the extension, although it would pass 100%. When i was adding attachments it would always ahve the name unit test.... I didnt use it before as Whenever i tried to change the extension over on the VF page I would get an error, I did not realise i had to change the filename and body to be the same as in the extension. Once again, thankyou my friend