How to run junit test using java - java

Hello I'm launching a Junit test using java using this code:
JUnitCore junit2 = new JUnitCore();
Result result2 = junit2.run(thenameoftheclass.class);
but in my junit original test there are some "VM option" (the IDEA window of cofig for junit). this VM options have the configuration to loggin in an app, are like the user and password system params. But I am not able to insert those params when I am trying to run junit using just a java program

Well guys I was able to find the answer.
First of all you need to know that my project is not maven based.
This methods are cool --->
JUnitCore junit2 = new JUnitCore();
Result result2 = junit2.run(thenameoftheclass.class);
The problem was that when I was building the Application arguments in Intellij IDE I was filling the app password and user in the field "program arguments" but actually it was meant to be field in "VM Options" becase the java machine uses them in execution time

Related

Debugging Elasticsearch Plugin Integration Tests using IntelliJ and Gradle

I am building an Elasticsearch plugin based on this example. The plugin uses Gradle for building, testing, integration testing, etc. I want to use IntelliJ to set a breakpoint in an Elasticsearch integration test and debug it. I don't care if I run the Gradle build from within IntelliJ or outside of IntelliJ, but right now in both cases I'm running into issues. I'll describe my setup and the problems below:
First I download and build/test the example project via CLI:
$ pip install cookiecutter
$ cookiecutter gh:spinscale/cookiecutter-elasticsearch-ingest-processor
$ # keep all default settings when prompted...
$ gradle build # (also runs the test and integTest tasks)
This runs fine and the tests pass. I generate the IntelliJ project using Gradle ($ gradle idea) and open it in IntelliJ (shows a green Gradle icon on the directory). I use the default gradle-wrapper option and keep all other defaults when opening.
First problem: I open IntelliJ's Gradle window (view > Tool Windows > Gradle), but I don't see any of the Gradle tasks which I know are defined in the project. This doesn't seem to be a huge deal, but it would be nice to have them.
Now I want to run the tests within IntelliJ, so I open the tests in org.elasticsearch.plugin.ingest.awesome.AwesomeProcessorTests. This class extends the ESTestCase class from Elasticsearch but doesn't directly test any Elasticsearch API interaction.
Second problem: I run this test class using the little green triangles next to the class name. This fails with the error java.lang.RuntimeException: unable to install test security manager.
I figured out I can get around this by editing the run configuration and adding the VM argument -Dtests.security.manager=false. Now the simple tests run and pass, and I can also set breakpoints and debug them.
Now I want to directly test some Elasticsearch functionality, so I write a new integration test class that extends the class ESIntegTestCase. The test should hit the Elasticsearch _cat/plugins endpoint and check that the response string is non-empty.
public class SimpleIT extends ESIntegTestCase {
private RestClient restClient;
#Before
public void setUp() throws Exception {
super.setUp();
this.restClient = getRestClient();
}
#Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return Collections.singletonList(IngestAwesomePlugin.class);
}
public void testPluginInstallation() throws IOException {
Response response = restClient.performRequest("GET", "_cat/plugins");
String body = EntityUtils.toString(response.getEntity());
assertTrue(body.length() > 0);
}
}
When I run this via CLI (gradle integTest), the test passes. If I add a purposely-failing assertion (e.g. assertTrue(body.length() < 0), then the test fails. So that seems to work.
Third problem: When I run the same test from within IntelliJ (little green triangles), I get the error:
java.lang.IllegalArgumentException: no hosts provided
at __randomizedtesting.SeedInfo.seed([6F620686489164F5:270C27B7060A2BA1]:0)
at org.elasticsearch.client.RestClientBuilder.<init>(RestClientBuilder.java:68)
at org.elasticsearch.client.RestClient.builder(RestClient.java:124)
at org.elasticsearch.test.ESIntegTestCase.createRestClient(ESIntegTestCase.java:2261)
at org.elasticsearch.test.ESIntegTestCase.createRestClient(ESIntegTestCase.java:2248)
at org.elasticsearch.test.ESIntegTestCase.createRestClient(ESIntegTestCase.java:2242)
at org.elasticsearch.test.ESIntegTestCase.getRestClient(ESIntegTestCase.java:2236)
at org.elasticsearch.plugin.ingest.awesome.SimpleIT.setUp(SimpleIT.java:36)
This maps to the line where I call getRestClient(), meaning IntelliJ is blocking the Gradle/Elasticsearch integration testing setup which otherwise works from the CLI.
FWIW, I can still set a breakpoint on this line and debug the test class in IntelliJ and it will stop at the line.
Now I try to run the integration tests via CLI, set a breakpoint in IntelliJ, and use IntelliJ's remote debugger to attach to the tests.
I setup a new "Remote" run configuration, keeping the defaults (transport socket, debugger mode attach, host localhost, port 5005). On the "Search sources using module's classpath" option, I've tried all the possible settings.
I set a breakpoint on the getRestClient() line, and run Gradle with remote debugging options via CLI:
$ GRADLE_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005" gradle integTest
Fourth problem: As expected, the Gradle process suspends, showing Listening for transport dt_socket at address: 5005. I start the new remote task in IntelliJ, and then the Gradle process continues running. Unfortunately it runs all the way through and doesn't stop at the breakpoint. The tests still pass.
I've searched online extensively for different ways to attach to the Gradle debugger. I've seen many things about disabling the daemon, and I've also tried using --system-prop to pass the debug parameters, but nothing solves this problem so far.

Spock unit tests in grails for individual test does not work in intellij

I am trying to run individual spock unit tests using intellij idea.
Consider:
// rest of code
def "Test Something"() {
// test code below
}
In above test, when I goto the test body and right context menu, I get two kinds of tests for Test Something. One is the grails test and other is the junit test.
Referring to this question, the accepted answer recommends using the jUnit runner. But using it, the code simply does not compile(probably because certain plugins and other classes are not available).
(I am not sure though as this is the desired behavior because I am just running a single test and not all tests. So wonder why is it compiling all classes ,including plugin classes not required by the test target class.)
Using the grails runner, I check the configuration and here is the screenshot:
So nothing looks wrong with the command there.
But the test on running gives Test framework quit unexpectedly error.
I try running same command from grails console(CMD windows) and it runs without any error message.
But on checking the output html files(in target/test-reports) I see that none of the tests actually ran!
So what is going on here and why are not individual tests running?
PS:
When I run All tests using test-app command, tests run as expected. Only individual (unit)tests are not running.
Part of the price paid for Spock's nice test naming, is that you can't specify an individual test to run anymore.
Here are some articles about it. The first seems pretty on-point:
Run a specific test in a single test class with Spock and Maven
This one isn't about running a single test, but has some relevance and talks about Spock's test-name conversions, plus Peter Niederwieser chimes in with comments:
Can TestNG see my Spock (JUnit) test results?
A workaround for this could be the #IgnoreRest annotation. Simply annotate the test you want to run with #IgnoreRest, and then specify that test class to run, and only the annotated test will run. http://spockframework.github.io/spock/javadoc/1.0/spock/lang/IgnoreRest.html
Try using the grails unit test and add the following in the command line part:
-Dgrails.env=development
This will run the test as we change the running environment to development . Hope this will help to everyone facing such problems.

How do I integrate my appium tests in jenkins

I have a Windows 7 and I am testing an Android Native app using Appium using Java. The framework used is TestNG. I would like to use Jenkins for running smoke testing for builds available in Jenkins. My desired capabilities are listed in #Before. Can we dynamically paremetrize all of the following in Jenkins?
“automationName”
“platformName”
“platformVersion”
“deviceName”
“app”
“appPackage”
“appActivity“
If so How?
Now if I dynamically parametrize, what would happen to the desired capabilities in my script #Before
its quite simple,
in your class you have to include the code which will listen for input from jenkin job.
String deviceName = PropertyUtils.getProperty("deviceName");
and while you configure your job in Jenkins you have to include string parameter like the image below,
and while building, pass the parameter you want for each build,
The value which you passing above will be processed in your code.

Why is my JUnit test not running in Eclipse?

It's been 3 hours now and I still didn't find a solution, even though I seem to have read all related questions already.
I am building an Android application and I just want to create a couple of simple Unit Tests that test my basic functions. I don't need to test any Android related logic or activity features.
So I have created a new directory in my solution in which I have created a new JUnit Test Case.
To keep things simple my test methods are not testing much yet, but even when doing a Right Click > Run As > JUnit Test, it's not doing anything.
As you can see in my screenshot the JUnit pane on the left shows my test is terminated but does not show any test that has been executed.
I have created a simple Unit Test in a new Java Project and then it's working. If I repeat the same steps in a new Android Application Project it's not working.
What do I need to do to run my simple Unit Tests?!
Thanks!
(My Compiler Compliance Level is 1.6)
Go to Window -> Show View -> Error Log to see what the actual error is.
For my case it was No test found with test runner 'Junit 5'.
Then one can google for respective solution.
You either don't have JUnit on the build path or you don't have the library (jar) at hand. Make sure both are in place.
I think you will need an unit test suite if all else fails:
#RunWith(Suite.class)
#SuiteClasses({ GpsLocationTest.class })
public class AllTests {
}
There is one more thing you can do: check whether you import the right #Test annotation. Restart Eclipse and clean your project if the problem persists.
You may want to refer to the vogella guide about unit testing.
You can use AndroidTestCase, which inherits from junit.framework.TestCase, not org.junit.Test.
This is related to Memory Issue.
Simply add these line in VM argument:
right click on Junit Test -> Run as -> Run Configuration -> Arguments -> add "-XX:MaxPermSize=512m" under VM argument
The java Build path of the project has Junit4.jar lets say and the Run configuration for the test you are running has Junit5 - then it causes to terminate and nothing happens.

How to execute ant using java and captured the output?

I have an ant build file that contains JUnit test suite that I would like to execute.
Currently I just right click and run the build file from Eclipse.
I want to write a java code that can execute the ant build file automatically.
So I just run the code and ant will be executed.
Second is I want to capture the test result. Currently the result is based on JUnit HTML report.
I want to make my own simple test report. I read there is JUnitResultFormatter but I can't
find the instructional step by step how to use it. Can anyone point me the reference?
The easiest way to do that is to use the JunitCore class from java. It is not advised to call the main from ant directly, see the Junit Faq, and http://www.answerspice.com/c119/1497833/how-do-i-run-junit-tests-from-inside-my-java-application.
It is very common to define a main like this for each test case, to be able to run the tests individually from command line. I usually also change the logging settings in those methods, to get more information when I run a single test manually than from within ant.
In order then to create a custom report, you will have to implement a RunListener that creates your report, and register it, as described in the javadoc:
public void main(String... args) {
JUnitCore core= new JUnitCore();
core.addListener(new RingingListener());
core.run(MyTestClass.class);
}
Your listener will then be called before and after each test run, and passed descriptive information about the test that is about to run, and how the test went once it is done.

Categories

Resources