I have code for this before update trigger and I have to write the test method for it. I am completely new to apex and this is the first trigger I wrote but not getting how to write test method for it.
Code
The screenshot doesn't provide much of the details, but I am writing the test class based on the info you have shared.
Tweak the code based on how you have implemented the trigger.
#isTest
private class StatusCloseDateTest {
#isTest static void testStatusCloseDate() {
yourObjAPIName__c rectoupdate = new yourObjAPIName__c();
rectoupdate.name = 'testValue';
/* based on the type of trigger (insert,update,delete) you wrote
perform the dml action.
EG:insert rectoupdate/update rectoupdate/delete rectoupdate
*/
insert rectoupdate;
/* when you perform this dml operation, the trigger will get
invoked and the code you have written in the trigger will
be covered by this test class method.
*/
}
}
Run the test methods in this class.
In the Developer Console, click Test | New Run.
Under Test Classes, click "StatusCloseDateTest".
To add all the test methods in the "StatusCloseDateTest" class to the test run, click Add Selected.
Click Run.
For more details refer : Trailhead
Related
For Nunit Test order, is it possible to have multiple order positions for a test, such as [Test, Order(3, 10, #...)]?
Scenario - I am automating a multi-page online application using end-to-end ordered tests (with Asserts verifying entered info), and I want to verify info on previous pages is still there - saved - when I click Back button (basically, reverse-check the end-to-end starting from last page of application using Back button to navigate). I want to reuse Asserts already there instead of rewriting the same ones again.
I have tried the following as an experiment, which generate build error CS1729 - 'type' does not contain a constructor that takes 'number' arguments:
[Test, Order(66, 67)]
**
[Test, Order(66)(67)]
**
[Test, Order(66), Order(67)]
**
[Test, Order(66)]
[Test, Order(67)]
I Googled this, and I don't see anything that does the above, so I assume it is not possible, but want to confirm. I cannot remove the Order attribute as that's part of the requirement for this test.
No, that's not possible. The OrderAttribute sets a property, which is a simple integer.
To simulate the effect you want, you may do the following:
Extract all the code of the test in question to a separate method, called by the test. It should have the same signature as the test.
Replicate the test you want to repeat, using a different name.
Assign a different order attribute to each test.
Assuming a signature of void TestMethod() it will look something like this...
[Test, Order(66)]
public void MyTest66()
{
MyRealTest();
}
[Test, Order(67)]
public void MyTest67()
{
MyRealTest();
}
private void MyRealTest()
{
// Your actual test code goes here
}
Note... this is a really ugly hack but the only guaranteed way I can think of to do what you indicate is required.
I'm trying to figure out how to run some code before and after all my cucumber tests run.
I've been tracking down a bug for a few days where some our processes create jobs on a server, and don't properly clean it up. It's easy to miss so ideally I don't want engineers to have to manually add a check to every test.
I was hoping there'd be a way to put a hook in before any tests ran to cache how many jobs exist on the server, then a hook at the end to ensure that the value hasn't changed.
I know this isn't really the best way to use cucumber, as that is more of a system test type thing to do, but doing it this way would be the best way to fit it into the existing infrastructure.
Use #BeforeClass and #AfterClass annotations in your run file.
#RunWith(Cucumber.class)
#Cucumber.Options(
format = {"json", "<the report file>"},
features = {"<the feature file>"},
strict = false,
glue = {"<package with steps classes>"})
public class TestRunFile {
#BeforeClass
public static void getJobNumbersOnServerBeforeStarting() {
//Implement logic
}
#AfterClass
public static void getJobNumbersOnServerAfterCompletion() {
//Implement logic
}
}
How about using tagged hooks.
#Before("#jobCheck")
public void beforeScenario() {
// actions
}
#After("#jobCheck")
public void afterScenario() {
// actions
}
And then for each scenario that requires this check, add #jobCheck before the Scenario definition as below.
Feature: Some feature description
#jobCheck
Scenario: It should process a sentence
// The steps
More on JVM hooks here: https://zsoltfabok.com/blog/2012/09/cucumber-jvm-hooks/
I'm using a GivenStories for executing Login scenario which is located in different story.
I was wondering if there is a way to use something similar in order to execute a logout story which is also located in different story than one I actually executing.
I know that I can do some tricks with #before/after annotations , but the question is if I can execute a "post" story
Thanks
Based on the jBehave annotation documentation a post story step can be implemented by annotating a step class method with #AfterStory (or #AfterStories if you want to execute only after all stories complete). The #AfterStory method will execute regardless of whether your executing story contains a step from the related step class (i.e. is guaranteed to execute after every story - see below for restricting to given stories).
The #BeforeStory and #AfterStory annotations allow the corresponding
methods to be executed before and after each story, either a
GivenStory or not:
#AfterStory // equivalent to #AfterStory(uponGivenStory=false)
public void afterStory() {
// ...
}
#AfterStory(uponGivenStory=true)
public void afterGivenStory() {
// ...
}
This is the answer I got from the jbehave dev channel.
Hi,
there is no such mechanism, but you could:
use the Lifecycle to execute steps (not stories) after the execution
of a scenario (executed after each scenario) have a final scenario
which invokes the given stories
Actually I am executing my selenium test by reading test case data from excel.I wanted to fetch whether the test result is Passed or failed after execution of my first test case and write it in front of test case then y second test case and write it in front of test case and so on .
Before execution of my test case excelsheet screenshoot
http://i.stack.imgur.com/L2LNz.png
after execution of my test cases excelsheet screenshoot
http://i.stack.imgur.com/mMivW.png
You can fetch the results using TestNG. TestNG contains default listeners which reads if your test passed/failed/was skipped.
To set this data in excelsheet you need to create a class that implements from ITestListener
public class ExcelListener implements ITestListener
If you use any IDE, you should see a warning about need of creating unimplemented methods. Allow system to create them and you should see methods like
#Override
public void onTestSuccess(ITestResult result) {
// TODO Auto-generated method stub
}
Then all you have to code is
1. Open excel file
2. Find the right column
3. Insert status
To do that I recommend using Java Excel API.
To read existing excelsheet you need to provide absolute path, workbook name and a sheetname. Here's my code for method getExcel
public void getExcel(String filePath, String sheetName, String fileName) throws BiffException, IOException {
String absolutePath = filePath.concat("/").concat(fileName);
file = new FileInputStream(new File(absolutePath));
workbook = Workbook.getWorkbook(file);
worksheet = workbook.getSheet(sheetName);
}
After getting an excel file, you need to iterate through data.
You can provide exact column and row.
Hope it helps!
EDIT:
Place a listener like this
#Listeners(MyExcelListener.class)
public class MyTestClass {
}
I am working with a plugin in symfony 1.4, and would like to add a listener for a task from another plugin. When the user does php symfony doctrine:build I want my plugin to run a task of its own. Where/how do I register the listener? I did not have any success with http://www.symfony-project.org/gentle-introduction/1_4/en/17-Extending-Symfony
Thank you!
Actually you can hook your code to any symfony task. Have a look at sfBaseTask::doRun method. Any task, when is executed, emits 2 events: command.pre_command and command.post_command.
Create a class to store you code, for example:
class toolkitEvents
{
static public function commandPostEventHook(sfEvent $event)
{
$task = $event->getSubject();
if ($task->getFullName() === 'doctrine:build')
{
//do stuff or call another task
}
}
and connect this method to a dispatcher when initializing your plugin:
class yourPluginConfiguration extends sfPluginConfiguration
{
public function initialize()
{
$this->dispatcher->connect('command.post_command', array('toolkitEvents', 'commandPostEventHook'));
}
I don't think there is a suitable event for this (like the cache clear one).
My suggestion would be either to accept that it needs to be two tasks, or if you use build that often, create a wrapper task to call one first then the other - doctrine:build is a good example of how to do this. Or a bash/batch script!