How can one execute Java code using Gatling load testing framework? - java

I am evaluating different load testing tools. After trying JMeter and having two exceptions when running and viewing the test result, I would like to give Gatling a spin. Reading to various resources I fail to find an idea how to execute once own Java Code.
I understand that Gatling is written in Scala but it runs on a JDK and Scala is able to incooperate/call Java code. So the question is, what does it take to combine both and if there are any resources available.

You can import and call your java class in any scala class.
So... Example:
I have scala class with gatling scenario
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import utils.NewRandom
class Example extends Simulation {
val protocol = http.baseUrl("https://httpbin.org")
val request = http("get request")
.get("/get")
val scn = scenario("Http bin scenario")
.exec(request)
before({
println(s"java random ${new NewRandom().getJavaRandom}")
})
setUp(
scn.inject(
atOnceUsers(1)
).protocols(protocol)
)
}
Gatling has before() and after() methods for execute before and after load run https://gatling.io/docs/current/general/simulation_structure/#hooks
If you pay attention on above code (in before() method) you will see the line where I create java object and call method.
Java class:
package utils;
import java.util.Random;
public class NewRandom {
public Integer getJavaRandom() {
return new Random().nextInt();
}
}

Related

cucumber feature file to run in order

Problem: I need to run my cucumber .feature file to execute in an order defined by me and rather not to run in the default order which is the folder structure.
I am running Appium for Android Native Apps built using cucumber .features file.
on windows machine, running on actual devices.
Now my Runcuckes file looks like below:
package runner;
import org.junit.runner.RunWith;
import org.testng.annotations.Test;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import cucumber.api.testng.AbstractTestNGCucumberTests;
//#RunWith(Cucumber.class)
#CucumberOptions(features = { "src/test/java/features" },
glue = { "Steps" },
monochrome = true,
tags = { "#CustomerInsightsSurveyPopupGiveFeedback,"
+ "#TestAccountSceanrios"
+ "#ShortlistPage,"
+ "#SavedSearchesPage,"
+ "#SearchResultPage,"
+ "#Short,"
+ "#SuggestedSearch" })
// public class RunCucke {
public class RunCucke extends AbstractTestNGCucumberTests {
}
Running your features or scenarios in order is Cuking the WRONG way.
In all testing linking one test to another is an anti-pattern. It makes your tests fragile and difficult to debug. Each test should be independent of every other test.
In Cucumber you use Givens to setup the state of your scenario. When's to actually do something. Then's to check your results. Your scenarios Given's should include everything needed to setup your application so you can do your When.
Cucumber encourages you to run your scenarios in a random order, and to reset just about everything between each scenario. Don't work against this, you will make things much more difficult if you do.

Cucumber:- Unable to generate step definitions by running feature file as well as testrunner class

I am trying to generate the step definitions from my feature file and as well as I have also designed test runner class but upon execution both give output on console as :-
0 scenarios
0 steps
0m0s.000s
Even though my feature file contains scenarios and steps.
Remove the colon (:) after the keywords (Given, When, etc) in your feature file.
Since you haven't shared any code or much details as to what you've done the only assumption that I can make is you have done something wrong in your testrunner class.
#RunWith(Cucumber.class)
#CucumberOptions(
features = "Feature"
,glue={"stepDefinition"}
)
public class TestRunner {
}
in the features make sure the path to your feature files is correct. i.e. if they are stored at some other directory, provide the path for the same
Ex: features = {"src/test/java/features"}
Also, please share your project structure, your feature file and your testrunner class code if possible in case this doesn't work for you.
Actually my runner class file looks like this:-
package runner;
import org.testng.annotations.Test;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
#CucumberOptions(features={"src//test//resources//featurefiles"},glue= {"im801clsteps"},plugin={"html:target/cucumber-html-report",
"json:target/cucumber.json", "pretty:target/cucumber-pretty.txt"})
#Test
public class MainRunner extends AbstractTestNGCucumberTests {
}
And I am using testng not junit to run my tests,please let me know why I am wrong?

How to execute scala code using java

I am new to scala. I have a requirement to execute the scala class using java.
My exact requirement is: I need to pass the entire scala class (file) as an argument to the java jar. That jar should read the scala code and execute it. I have searched many sites but did not find the appropriate answer. Is there any way to do the same?
Thank you in Advance.
Besides of your motivation to do that, it is for sure possible (I did it using my IDE - sbt project)
I just made scala class as below:
import com.google.common.base.Objects
class Car(_color: String, _valid: Boolean) {
val color: String = _color
val valid: Boolean = _valid
override def toString = Objects.toStringHelper(this).add("color",color).add("valid", valid).toString
}
After that I made class with main method to test it.
public class Test {
public static void main(String[] args) {
Car test = new Car("test", true);
System.out.println("test = " + test);
}
}
It compiled without any problems and the result was like below:
test = Car{color=test, valid=true}
Scala has its own compiler scalac whereas java uses javac. Since scalac compiles to class file that java can read and assuming that you are only using java libraries in the class then you can load the class in java. So what you need is to call scalac to compile the scala file and then load the generate class file using ClassLoader

Model Based Testing android

I have generated test cases that I need to execute. I have written my methods of the tests independent of each other and writing scripts to execute these methods via the adb is not helping since they dont execute in order of the sequence given it.
I would like to know a preferred approach to take ?
Or how I can automate these multiple tests via the adb.
I have realized all most out there tend to use the adb commands under their codes so dont know if there is a tool that may be of help. I am open to that as well
Thank you
I think there's a problem with your test case structure.
Test cases should be independent of each other and that is not something that is only encouraged but often a necessity to ensure integrity of your tests.
If you have dependent actions then they need to be bundled in the same test case or you need to look at dependency injection/mocks.
That being said, JUnit4 has a #FixMethodOrder annotation which you can add to run the tests in order.
import org.junit.runners.MethodSorters;
import org.junit.FixMethodOrder;
import org.junit.Test;
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SampleTest {
#Test
public void firstTest() {
System.out.println("first");
}
#Test
public void secondTest() {
System.out.println("second");
}
}

JUnit testing result of a program

This is a very basic question for JUnit testing. I wrote a program which calculate the new position of a dot by given instructions for moving. The program is working properly but I have to write a JUnit test for check the result and I don't know how.
Write a test method for every dot movement that you want to check. In each test method you call your method and then compare the actual result with the expected result.
Try something like this, using JUnit 4.x :
package org.dotmover;
import org.junit.Assert;
import org.junit.Test;
public class DotMoverTest {
#Test
public void testDotMoverForward() {
final DotMover dotMover = new DotMover(...);
final int newPos = dotMover.move(...);
final int expectedNewPos = ...;
Assert.assertEquals(expectedNewPos, newPos);
}
}
Add JUnit to classpath of you project. The create JUnit testcase. It is just a class that extends TestCase.
If you are using JUnit prior to v 4.0 each test method must start from word test, e.g.
testPosition(), testMoviing() etc.
If you are using version 4 and higher the test methods must be targeted with annotation #Test.
Now write your testing scenario. User static assertXXX() methods of class Assert to verify that your program is working.
Good luck and happy TDD!

Categories