Unable to run test with VertxUnitRunner - vert.x

I am new to the Vertx Unit and trying to run the below example, but with no luck
#RunWith(VertxUnitRunner.class)
public class DemoTest {
#BeforeClass
public static void before(TestContext context) {
System.out.println("before");
}
#Test
public void testSomethingElse(TestContext context) {
System.out.println("testSomethingElse");
}
#AfterClass
public static void after(TestContext context) {
System.out.println("after");
}
}
I am running using mvn clean test
Receiving the following output
Running com.example.DemoTest
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec

It was solved by adding the JUnit dependency.

Take a look as well to the maven-surefire-plugin (https://maven.apache.org/surefire/maven-surefire-plugin/index.html) and add it to your pom.xml, here I post some mvn commands to test:
# Run all the unit test classes.
$ mvn test
# Run a single test class.
$ mvn -Dtest=TestApp1 test
# Run multiple test classes.
$ mvn -Dtest=TestApp1,TestApp2 test
# Run a single test method from a test class.
$ mvn -Dtest=TestApp1#methodname test
# Run all test methods that match pattern 'testHello*' from a test class.
$ mvn -Dtest=TestApp1#testHello* test
# Run all test methods match pattern 'testHello*' and 'testMagic*' from a test class.
$ mvn -Dtest=TestApp1#testHello*+testMagic* test

Related

Test Discovery of Dynamic Tests in Eclipse Slow

Having a test class like this
public class VerySimpleFactory {
#TestFactory
public Stream<? extends DynamicNode> someTests() {
DynamicContainer container1 = DynamicContainer.dynamicContainer("A",
Arrays.asList(t("A1"), t("A2"), t("A3"), t("A4"), t("A5")));
DynamicContainer container2 = DynamicContainer.dynamicContainer("B",
Arrays.asList(t("B1"), t("B2"), t("B3"), t("B4"), t("B5")));
DynamicContainer container3 = DynamicContainer.dynamicContainer("C",
Arrays.asList(t("C1"), t("C2"), t("C3"), t("C4"), t("C5")));
DynamicContainer container4 = DynamicContainer.dynamicContainer("D",
Arrays.asList(t("D1"), t("D2"), t("D3"), t("D4"), t("D5")));
return Arrays.asList(container1, container2, container3, container4).stream();
}
#Test
public void t1() throws Exception {
Thread.sleep(1000);
}
#Test
public void t2() throws Exception {
Thread.sleep(1000);
}
public DynamicTest t(String name) {
return DynamicTest.dynamicTest(name, () -> Thread.sleep(1000));
}
}
the Tests having a #Test annotaiton are discovered instantly by JUnit View, but the tests from TestFactory are discoverd at runtime, each after the last test was completely executed. This leads to a changing and "jumping" JUnit view. Also I cannot select a special test I'm interested in to be executed as single test, until all previous tests were executed.
It would be much nicer if all dynamic tests were shown in JUnit view also at beginning of test execution.
If this doesn't happen, is it a problem of JUnit 5, eclipse or my code?
Dynamic tests are dynamic. Not static.
It is not possible to know before-hand which and how many tests will be generated by #TestFactory annotated method ... actually, it may produce tests in an eternal loop.
Copied from https://junit.org/junit5/docs/current/user-guide/#writing-tests-dynamic-tests-examples
generateRandomNumberOfTests() implements an Iterator that generates
random numbers, a display name generator, and a test executor and then
provides all three to DynamicTest.stream(). Although the
non-deterministic behavior of generateRandomNumberOfTests() is of
course in conflict with test repeatability and should thus be used
with care, it serves to demonstrate the expressiveness and power of
dynamic tests.

error when running groovy test suite

created two tests in groovy able to run them indecently as groovy test cases but when I create a test suite and run them like groovy MyTestSuite.groovy on cmd line I get the below error:
F.F
Time: 0
There were 2 failures:
1) warning(junit.framework.TestSuite$1)junit.framework.AssertionFailedError: No tests found in mypack.ArithmeticTest
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1318)
at org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:927)
at org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod(InvokerHelper.java:77)
at groovy.lang.GroovyShell.runJUnit3TestSuite(GroovyShell.java:370)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:277)
at groovy.lang.GroovyShell.run(GroovyShell.java:502)
at groovy.lang.GroovyShell.run(GroovyShell.java:491)
"a.txt" 53L, 3408C
The test suite class is as follows
package mypack
import junit.framework.TestSuite
import junit.framework.JUnit4TestAdapter
public class myTestSuite extends TestSuite {
// Since Eclipse launches tests relative to the project root,
// declare the relative path to the test scripts for convenience
private static final String TEST_ROOT = "src/mypack/";
public static TestSuite suite() throws Exception {
TestSuite suite = new TestSuite();
GroovyTestSuite gsuite = new GroovyTestSuite();
suite.addTestSuite(gsuite.compile("/Users/barumugham/Documents/workspace/Groovy/UnitTestGroovy/src/mypack/ArithmeticGroovy.groovy"));
suite.addTestSuite(gsuite.compile("/Users/barumugham/Documents/workspace/Groovy/UnitTestGroovy/src/mypack/ArrayTest.groovy"));
return suite;
}
}
ArithmeticGroovy.groovy
package mypack
import org.junit.Test
import static org.junit.Assert.assertEquals
class ArithmeticTest {
#Test
void additionIsWorking() {
assertEquals 4, 2+2
}
#Test(expected=ArithmeticException)
void divideByZero() {
println 1/0
}
}
when i run it through eclipse i get initializationerror. I am new to groovy any help is appreciated
Groovy seems to be confused if you confuse (pardon the pun :-) JUnit3 and JUnit4 style. Using TestSuite and addTestSuite is JUnit3 style and it should be coupled with classes derived from TestCase (or ultimately Test). Mixing JUnit4 annotation style won't work in this setup as exemplified in this post.
You should select either JUnit3 or JUnit4 style for your tests (where I personally tend to prefer JUnit4).

TestNG - test executed but tests run = 0

I'm trying to run TestNG with Selenium in Eclipse.
When the Class file is Run As a TestNG Test, I'm getting the Tests Run = 0.
What could the issue be?
I have the testNg plugin & jar files configured.
testng.xml file:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="WikiTestSuite" verbose="3" parallel="methods">
<test name="WikiTestCase">
<classes>
<class name="com.selenium.WebDriverTest" />
</classes>
</test>
</suite>
Classes:
WebDriverTest class:
package com.selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
#Test
public class WebDriverTest extends BrowserInstance{
private void WebDrive() {
// navigate to specified browser
wDriver.get("http://www.wikipedia.com");
// assert wikipedia is opened
Assert.assertEquals(wDriver.getTitle(), "http://www.wikipedia.com");
System.out.println("Page title is correct. PASS!");
}
}
BrowserInstance class:
package com.selenium;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
public class BrowserInstance {
// initialize webdriver object reference
WebDriver wDriver;
#BeforeMethod
public void launchBrowser() {
wDriver = new FirefoxDriver();
System.out.println("In launchBrowser method");
}
#AfterMethod
public void closeBrowser() {
wDriver.close();
wDriver.quit();
System.out.println("In closeBrowser method");
}
}
Note:
When I run the testing.xml file as a TestNG Suite, the results are:
[TestRunner] Running the tests in 'WikiTestCase' with parallel mode:methods
[RunInfo] Adding method selector: org.testng.internal.XmlMethodSelector#7426dbec priority: 10
[TestClass] Creating TestClass for [ClassImpl class=com.selenium.WebDriverTest]
[TestNG] Running:
/Users/developer/Documents/workspace/SeleniumTest1/lib/testng.xml
[SuiteRunner] Created 1 TestRunners
[TestRunner] Running test WikiTestCase on 1 classes, included groups:[] excluded groups:[]
===== Test class
com.selenium.WebDriverTest
#BeforeMethod BrowserInstance.launchBrowser()[pri:0, instance:com.selenium.WebDriverTest#4979935d]
#AfterMethod BrowserInstance.closeBrowser()[pri:0, instance:com.selenium.WebDriverTest#4979935d]
======
===== Invoked methods
=====
Creating /Users/developer/Documents/workspace/SeleniumTest1/test-output/WikiTestSuite/WikiTestCase.html
Creating /Users/developer/Documents/workspace/SeleniumTest1/test-output/WikiTestSuite/WikiTestCase.xml
===============================================
WikiTestCase
Tests run: 0, Failures: 0, Skips: 0
===============================================
===============================================
WikiTestSuite
Total tests run: 0, Failures: 0, Skips: 0
===============================================
When I run the .java file as a TestNG Test, the results are:
[TestNG] Running:
/private/var/folders/q6/xqd3z8fx69z05pbclbx_bj740000gp/T/testng-eclipse--953413131/testng-customsuite.xml
===============================================
Default test
Tests run: 0, Failures: 0, Skips: 0
===============================================
===============================================
Suite
Total tests run: 0, Failures: 0, Skips: 0
===============================================
In both cases, the result is "Tests Run = 0".
Any help will be much appreciated.
Thanks!
EDIT:
I found the solution: I used a 'private' method in my .java file.
I was able to rectify the bug but I'm now wondering why TestNG does not run a private method.
Any input is welcome.
Thanks!
You should add "#Test" on test (WebDrive) method. also I am not sure if private Test run. So cross check with public
I checked, even the default access modifier does not work, you have to specify as public for testng to run your method.
You need to add #Test annotation to "private void WebDrive()" method or change the access modifier to public i.e from private void WebDrive(). to public void WebDrive()

Parameterized Groovy JUnit test-cases in Eclipse

I'm having trouble running a Parameterized Groovy JUnit test-case in Eclipse (see below for test code and environment details).
Symptoms
Right-clicking on the class in Package Explorer and doing Run As -> JUnit Test Case just brings up a dialog claiming "No JUnit tests found".
Right-clicking on the project and doing Run As -> JUnit Test Case runs all test-cases except the parameterized Groovy one.
Things I've tried
Ensuring a "normal" Groovy JUnit test-case runs. This works.
Ensuring a parameterized Java test-case runs. This works.
Manually creating a JUnit run configuration for this test-case. This works.
So
So I have an inconvenient workaround (3). But this isn't scalable, as this test-case still won't be included when I run all test-cases in the project.
Any ideas how I can get Eclipse/Groovy plugin/JUnit to automatically recognise my test-case?
Test-case code
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameters
#RunWith(Parameterized)
public class TestParams {
final int a
public TestParams(int a) { this.a = a }
#Parameters
public static Collection<Object[]> data() {
def cases = new Object[2][1]
cases[0][0] = 3
cases[1][0] = 4
Arrays.asList(cases)
}
#Test public void test() { println "a = $a" }
}
Environment
Eclipse Juno Service Release 2 (OSX)
Groovy-Eclipse 2.8.0
JUnit 4.10.0
this code works on my juno eclipse, junit 4.10 and groovy 2.0.6. i started with your code, but had to fool around with the imports as some of the annotations were red. i also had to add the .class to parameterized.
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameters
#RunWith(Parameterized.class) public class TestParams {
final int a
public TestParams(int a) { this.a = a }
#Parameters
public static Collection<Object[]> data() {
def cases = new Object[2][1]
cases[0][0] = 3
cases[1][0] = 4
Arrays.asList(cases)
}
#Test public void test() { println "a = $a" }
}

How can I use a JUnit RunListener in Eclipse?

I wrote a simple RunListener for JUnit which works quite well with Maven. I could register it for the maven-failsafe-plugin via
<properties>
<property>
<name>listener</name>
<value>com.asml.lcp.middleware.common.jboss.test.tps.TestDocumentationListener</value>
</property>
</properties>
and see the correct output from the listener.
Now I want to register the same RunListener in Eclipse to see the same output there, when I run the tests.
Is this possible? For testing purposes and to be consistent it would be nice to have the same output.
I have a set of tests that need a database to be executed. I want to create the database at the beginning of their execution and remove it at the end.
From maven I've also added a RunListener to the maven-surefire-plugin and it works fine. And I've also added a system property variable named ismaven. When I execute the test from maven this variable is initialized but when I execute the tests from the Eclipse, this variable is null (I access to the variable with System.getProperty).
<configuration>
<properties>
<property>
<name>listener</name>
<value>com.mycompany.MyRunListener</value>
</property>
</properties>
<systemPropertyVariables>
<ismaven>true</ismaven>
</systemPropertyVariables>
</configuration>
All my database tests inherit from a class that has a #BeforeClass and an #AfterClass methods. These methods check if the test is being executed by Maven or by the Eclipse checking the value of the ismaven property. If the test is being executed by maven, the ismaven property has a value and they do anything. But is the test is being executed by the Eclipse, the ismaven variable is null and they starts (#BeforeClass) or stops (#AfterClass) the database:
#BeforeClass
public static void checkIfStartDatabase() {
String ismaven = System.getProperty("ismaven");
// If it is not maven, start the database
if (ismaven == null) {
startDatabase();
}
}
#AfterClass
public static void checkIfStopDatabase() {
String ismaven = System.getProperty("ismaven");
// If it is not maven, stop the database
if (ismaven == null) {
stopDatabase();
}
}
This solution doesn't solve 100% your problem but if you implement it you can execute (and debug) all the tests of one JUnit class using the Eclipse and you can also execute all the tests of your project using Maven with the guarantee that you will execute once a piece of code before or after the execution of all your tests.
Yes, it is possible. Basically you have to implement your own Runner and inside the run method, you can add a custom run listener. I figured this out based on this post, point 2.
Here is my listener
public class TestLogger extends RunListener
{
public void testFinished(Description description)
{
System.out.println("Successful " + description.getMethodName());
}
}
and here is my Runner
public class TestRunner extends BlockJUnit4ClassRunner
{
public TestRunner(Class<?> klass) throws InitializationError
{
super(klass);
}
#Override
public void run(RunNotifier notifier)
{
notifier.addListener(new TestLogger()); // THIS IS THE IMPORTANT LINE
super.run(notifier);
}
}
and here is my actual junit test
#RunWith(TestRunner.class) // THIS LINE IS ALSO IMPORTANT
public class MyTest1
{
#Test
public void Test1() throws Exception
{
if (Math.random() < .5) throw new Exception("ouch");
assertFalse(Math.random() < .5);
}
}
You can run MyTest1 or the Test1 method using the context menu in Eclipse and it will invoke the testLogger as you would expect.
I did some more reseatrch on this. As far as I can see now, there is no way to add a run listener too the JUnit runs in Eclipse.