DependONGroups annotation testng - java

I am using DependOnGroups parameter in #test annotation.the code looks like,
#Test(groups={"datacompare"},dependsOnGroups = {"AzkabanFlow"})
Now the requirement is we need to run the test only for the group datacompare which is done by specifying the maven parameter,
clean test site -DtestGroup=datacompare
Since the above group has dependency with the group azkban flow, i am getting the error
[ERROR] DependencyMap::Method "DataValidationTestSuite.data_Comparison(java.lang.reflect.Method)[pri:0, instance:com.kohls.test.automation.framework.testsuite.DataValidationTestSuite#1608e1a]" depends on nonexistent group "AzkabanFlow"
Can someone suggest me a way to run the test for datacompare without removing the parameter DependOnGroups and also not calling the particular group mentioned in dependOnGroup parameter in maven parameter for test run.

You might want to change your #Test annotation to something like below
#Test(groups={"datacompare"},dependsOnGroups = {"AzkabanFlow"}, ignoreMissingDependencies=true)
This would cause TestNG to ignore missing dependencies and hopefully it should solve your problem as well.
Javadocs for the same can be referred here.

Related

WebDriver is NULL when start more then one test case

I have a problem. There are 2 Test classes in my code and when I run each 1 test case manually both working fine. But when execute tests with maven only one test is executed successfully and other gives me error
java.lang.NullPointerException
at com.selenium.course.tests.ProductTests.executeProductTest(ProductTests.java:17).
Expected behavior: all tests should execute with maven.
Here is my code = https://github.com/Dermenji/SeleniumCourse
In your base calass where you have get methods for the driver, you need a "global" class variable which will be static.
public static WebDriver driver;
Selenium also works with annotations which they have not yet implemented.
For me, many test cases caused errors after I added annotations everything went well.
https://www.browserstack.com/guide/testng-annotations-in-selenium
maybe this Site helps u.
You should use same driver instance for all test classes
The solution is you can use util class for driver instance

JUnit 5 #EnabledIfSystemProperty doesn't work as expected

I migrated my test from JUnit 4 to JUnit 5. All works fine but the translation of my previous annotation:
#IfProfileValue(name = "run.import.tests", values = {"true"})
into
#EnabledIfSystemProperty(named = "run.import.tests", matches = "true")
doesn't work as expected. Before the migration I runned my tests passing the argument
-Drun.import.tests=true
only if I passed it they were runned. With Junit 5, even with annotation #EnabledIfSystemProperty(named = "run.import.tests", matches = "true") the test is runned even if the argument run.import.tests is not set.
Am I doing something wrong?
To make it work an "opposite" annotation has to be added, so both of them together look like this:
#EnabledIfSystemProperty(named = "run.import.tests", matches = "true")
#DisabledIfSystemProperty(named = "run.import.tests", matches = "(?!true)")
I've checked it and the test class is disabled if the run.import.tests property is not set or if it is set to any other value than true; if the value is set to true - the test class is not disabled.
Curiously, the documentation of #EnabledIfSystemProperty states:
If the specified system property is undefined, the annotated class or method will be disabled.
Yet it does not work that way and it may be a bug. I will try and debug the JUnit classes and if I create an issue on their GitHub, I will link it here.
I've gone through the code and tested it a few more times - here is the summary:
When I run the test using Maven (mvn test), the annotation #EnabledIfSystemProperty alone works fine - the test is run only when I add -Drun.import.tests=true argument. #DisabledIfSystemProperty is not needed in that case.
When I run the test using IntelliJ's Run XxxTest handling of the property worked fine only if both annotations were present. After some debugging I came across JupiterTestEngine - a class that is run by external launchers (Maven, IntelliJ, any other). It seems that IntelliJ adds a property to it's test launcher: junit.jupiter.conditions.deactivate, which is usually useful - thanks to that we can run even tests that are disabled with conditional annotations locally, ignoring them. The value of the property is org.junit.*Enabled*Condition when #DisabledIfSystemProperty is not present and org.junit.*Disabled*Condition when it is - the conditions are JUnit's extensions that resolve disabled state for the test.
The functionality described in (2) is usually useful, but in your case it made it look like the annotation is not working. It actually is working, but IntelliJ just bypasses it.

Unable to run JUnit test with PowerMockRunner

I have a Gradle based Java project were I now want to mock a private method using PowerMock. The problem is that I am not able to use the PowerMockRunner as I always get the following exception when I add the #RunWith(org.powermock.modules.junit4.PowerMockRunner.class) annotation.
Error:
org.powermock.reflect.exceptions.FieldNotFoundException: Field 'fTestClass' was not found in class org.junit.internal.runners.MethodValidator.
at org.powermock.reflect.internal.WhiteboxImpl.getInternalState(WhiteboxImpl.java:581)
at org.powermock.reflect.Whitebox.getInternalState(Whitebox.java:308)
at org.powermock.modules.junit4.internal.impl.testcaseworkaround.PowerMockJUnit4MethodValidator.validate TestMethods(PowerMockJUnit4MethodValidator.java:79)
at org.powermock.modules.junit4.internal.impl.testcaseworkaround.PowerMockJUnit4MethodValidator.validate InstanceMethods(PowerMockJUnit4MethodValidator.java:49)
at org.junit.internal.runners.MethodValidator.validateMethodsForDefaultRunner(MethodValidator.java:51)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.validate(PowerMockJUnit44RunnerDelegateImpl.java:108)
...
This are my test dependencies:
testCompile 'junit:junit:4.+',
'org.powermock:powermock-core:1.5.6',
'org.powermock:powermock-module-junit4:1.5.6',
'org.powermock:powermock-api-mockito:1.5.6'
The test itself fails also when completely empty (initialization error):
#RunWith(PowerMockRunner.class)
public class SomeTest {
#Test
public void testSomething() {
}
}
Any ideas what might be wrong? Other tests using PowerMock are working fine (none of them uses the PowerMockRunner).
Greetings and thanks for any help!
Ben
This is a bug that occurs when you use JUnit 4.12 and PowerMock < 1.6.1. The problem is solved in PowerMock 1.6.1. Please update your dependencies accordingly
testCompile 'junit:junit:4.12',
'org.powermock:powermock-core:1.6.1',
'org.powermock:powermock-module-junit4:1.6.1',
'org.powermock:powermock-api-mockito:1.6.1'
If you cannot upgrade PowerMock then you can use JUnit 4.11.
testCompile 'junit:junit:4.11',
'org.powermock:powermock-core:1.5.6',
'org.powermock:powermock-module-junit4:1.5.6',
'org.powermock:powermock-api-mockito:1.5.6'
Could you please add further lines of the stacktrace, which uncover more details about the problem.
There has been a bug logged against PowerMock: https://code.google.com/p/powermock/issues/detail?id=531
It appears that JUnit changed some of its internal field names that PowerMock was accessing via reflection, thus breaking the ability for PowerMock to properly inject itself.
Check what Stefan said, and above that you also need to add
#PrepareForTest({<The class/es you are Mocking>, ...})
without the prepare for test, PowerMockRunner won't know which class is mocked.
There may also exist dependencies on classpath that override a JUnit specific class which contains JUnit's version. This leads to incorrect version comparison results in PowerMock. For instance, I had com.google.android.tools:dx:1.7 on classpath (came from hunspell library). It overrides following method return result:
junit.runner.Version.id() => "3.8.1"
Usually it should return something like "4.12" or "4.11" etc.

Testing ServiceLoader in Eclipse Plugins

I have four Eclipse plugin projects (create a new Java Project, right-click, configure, Convert to Plugin Project) in my workspace. The first (my.runtime) contains an interface (MyFactoryInterface) and a class (MyClient) that defines a method (List<String> getAllFactoryNames()) that loads all implementations of that interface via java.util.ServiceLoader and calls a method (String getName()) on them, collecting the results and returning them in a list.
To test that class, I have a JUnit test in the second project (my.runtime.test, set up with my.runtime as Fragment-Host), checking if the name returned by a dummy implementation(MyDummy, returning "Dummy") I have in the my.runtime.test project is in the list returned by MyClient.getAllFactoryNames(). So far, it works fine.
In the third project (my.extension, with my.runtime as dependency) I have a class (MyHello) that uses the names returned by MyClient.getAllFactoryNames() to return a list of greetings ("Hello "+name).
Again, to test this, I have a project (my.extension.test, with my.extension as Fragment-Host) containing another Implementation (MyWorld, returning "World" as name) and a JUnit test case checking if "Hello World" is in the greetings returned by MyHello.getGreetings(). This test fails, as MyClient still only finds the MyDummy implementation, and not the MyWorld implementation. Both implementations are accompanied by matching entries in META-INF/services/my.runtime.MyFactoryInterface files.
I currently use the following code to load the implementations:
ServiceLoader<MyFactoryInterface> myFactoryLoader = ServiceLoader.load(MyFactoryInterface.class);
for (MyFactoryInterface myFactory : myFactoryLoader) {
I know that I can supply a ClassLoader as a second argument to ServiceLoader.load, but I have no idea how to get one that knows all plugin projects... any suggestions? Or is ServiceLoader not the right tool for this problem?
If anyone stumbles over the same problem: the combination of ServiceLoader.load(MyFactoryInterface.class, Thread.currentThread().getContextClassLoader()) in MyClient and Thread.currentThread().setContextClassLoader(MyWorld.class.getClassLoader()); in the second test did the job.

Is it possible to exclude multiple categories in JUnit 4?

I would like to do something like:
#RunWith(Categories.class)
#Categories.IncludeCategory(Small.class)
#Categories.ExcludeCategory({Manual.class, NonFunctional.class})
#Suite.SuiteClasses(AllTests.class)
public class SmallTests {
}
but ExcludeCategories accepts only one class, not an array of classes.
This will be supported in JUnit 4.12 version, see https://github.com/junit-team/junit/blob/master/src/main/java/org/junit/experimental/categories/Categories.java
It looks like runtime-suite may provide another workaround/solution.
There's a JUnit 4 feature request for this:
https://github.com/junit-team/junit/issues/146
This link also suggests a workaround:
There is a not-so-beatiful workaround. You can get multiple includes or excludes
if you create an inheritance hierarchy of suites and exclude one category on
each level.

Categories

Resources