What does "Such methods are inherited unless they are overridden" means - testing

When JUnit5 explain #Test, the doc https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations said
Such methods are inherited unless they are overridden
What does this sentence means? I not see anything related to method inheritance at all

This means that if you have a test class A, in which you define a method test annotated with #Test, JUnit will execute test also in a test class B that inherits A. This does not apply if you override test in class B.
Example:
public class A {
#Test
public void testA() {
// ...
}
#Test
public void testB() {
// ...
}
}
public class B extends A {
#Override
public void testB() {
// ...
}
}
When executing test class A, testA and testB will be executed. When executing B, testA is inherited and will be executed. However, testB is overridden and not annotated with #Test and will not get executed.

Related

Is there a way to have the #MethodSource arguments parameters derived from another method

I have a Junit 5 non static parameterized test where I am using #MethodSource to get the test data.
The Method Source arguments are getting the data from other methods as below code. I made the #MethodSource non static by having a
#TestInstance(TestInstance.Lifecycle.PER_CLASS)
The fullDomain parameter in the #MethodSource is derived from the method getFullDomain() in the registrationPage which is also non static . I #Autowired the RegistrationPage in the BaseTest and using it by extending in my Junit5SampleTest Class
When I run the test the fullDomain is returning null. Is there a way to fix this or can I use any others like #ArgumentSource to fix this.
I will need to get the Arguments.of("Successful Regisration", fullDomain) data from different classes and its methods.
Base Test:
public class BaseTest {
#Autowired
protected RegistrationPage registrationPage;
}
Test Class:
#TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class Junit5SampleTest extends BaseTest {
String fullDomain = registrationPage.getFullDomain();
Stream<Arguments> registrationInputParameters() {
return Stream.of(
Arguments.of("Successful Regisration", fullDomain)
}
#ParameterizedTest()
#MethodSource("registrationInputParameters")
public void junit5(String testName, String fullDomain){
System.out.println(testName);
open(fullDomain);
}
}

How to skip a testNG class based on element visibility and switch to another class

I am using testNG for my selenium suite. There is a class having 35 test cases. But these test cases will execute only if a particular element is visible. If that element is not visible, the compiler goes through all the test cases. Is there any way that I could check that element visibility condition in an #BeforeClass annotation only. If an element is not visible, it should come out from that class and switch to the next one? It will save my time to go through all the test cases?
To achieve it use #Test annotation on class level and #BeforeTest to check element visibility so it will skip all test cases of class if it will not satisfy condition in #BeforeTest. See below code (it's tested and working).
#Test
public class SkipAllTestCases {
boolean elementNotVisible=true;
#BeforeTest
public void setUp() {
if (elementNotVisible) {
throw new SkipException("skipping test cases...");
}
}
public void test1() {
System.out.println("Test1");
}
public void test2() {
System.out.println("Test2");
}
public void test3() {
System.out.println("Test3");
}
}
Hope it will help.
You can use dependsOnMethods of TestNG Test annotation.
#Test
public void elementVisibleTest(){
//Fail or skip here
}
#Test(dependsOnMethods = {"elementVisibleTest"})
public void myOtherTest(){
//Do something
}
...
That means if elementVisibleTest fails or gets skipped all tests which depend on that test will be skipped too. The advantage of that would be that you can still have other tests in that class which will be executed (because they do not depend on elementVisibleTest).
One of the approach is add group to all such tests let say flow-1. Add before group method and throw exception if it doesn't match required condition. For example:
#BeforeGroups(groups="flow-1")
public void flow1() {
if(!requiredCondtionMatch) {
throw new SkipException("Flow not applicable");
}
}
If all tests falls under same class then you can use #BeforeClass as well.

Verifications involving non-mocked class with mocked abstract parent

A member of my team has written a JMockit-based test method that using a Verifications instance to assert a method was invoked on the UUT, which is not mocked, but extends a mocked abstract parent (it happens to be a Hibernate repository). The test passes, but my opinion, based on the JMockit documentation, is that only mocks should be used in a Verifications instance initializer. I think the result is a false negative but my team member insists it's a valid verification call. The test itself is simple, so I've recreated it using contrived objects:
package com.acme.dataacess;
public abstract class AbstractRepository {
public final T list(Class<T> clazz, final Collection<String> keys) {
.....
}
}
package com.acme.module.dataacess
public class FooRepository extends AbstractRepository<Foo> {
public List<Foo> list() {
return getFoos(null);
}
public List<Foo> list(Collection<String> keys) {
return list(Foo.class, keys);
}
}
public class FooRepositoryTest {
#Tested
private FooRepository uut;
#Mocked
private AbstractRepository mockAbstractRepository;
#Test
public void testFoo1() {
// Execute the test.
this.uut.list(null);
// Verify the results.
new Verifications() {
{
// I don't think this is a valid verification, because the goal
// of the test is to assert a delegated method in a non-mocked
// class was invoked.
uut.list(null);
}
};
}
}
Is this a valid JMockit verification?

Intellij reports code duplication while actually it's not

Here's the code. The code in method test and test2 are different because the parameter passed to Test constructor are different. Actually, if I change any parameter to null, intellij stops reporting the duplication. Is there any way to fix this?
---- Updated --------
I pass 2 functions doing totally different things but intellij still reports duplication
public class TestMain {
public void test(int a)
{
System.out.println("haha");
System.out.println("hahaa");
TestMain testMain = new TestMain();
new Test(testMain::test3);
System.out.println("hahaaa");
}
public void test2(int a)
{
System.out.println("haha");
System.out.println("hahaa");
TestMain testMain = new TestMain();
new Test(testMain::still_dup);
System.out.println("hahaaa");
}
public void test3(int a) {
System.out.println("abc");
}
public void still_dup(int a) {
String b = "edf";
b.toLowerCase();
}
public class Test {
Test(handler h) {
}
}
public interface handler<M> {
void entitySelector(int a);
}
public static void main(String[] args) {
TestMain test = new TestMain();
test.test(1);
System.out.println("-------");
test.test2(2);
}
}
I think the best way to fix this is to replace test and test2 by a single method. You don't have to distinguish what to pass the constructor because it's the current method. This might be the reason why code duplication is reported. The methods can be replaced by a single one without problems.

to run beforemethod before dataprovider

We are trying to automate the test cases to run in parallel .For this we are using Testng annotations, like #test ,#before method and #data provider. Problem we are facing is, for all the #test the #before method is running but only for the test having #data provider it is getting run after #dataprovider. That is the problem
Our code looks like this
Class Test()
{
Public Test()
{
// code
}
#Beforemethod
Public BeforeTest(){
//Initializing an object for a class where all methods in which we are running in the #Test methods
}
#Dataprovider
Public Dataprovider()
{
//code
}
#Aftermethod
Public Aftermethod()
{
Null the object created in the #before method
}
#Test
Public test1(){
//code
}
#Test(groups={"tests_verifyDataEntryScenarios"},enabled=true, dataProvider = “name”)
Public Test2()
{
//code
}
Problem coming with the ‘#Test test2()’, as it has data provider, instead of calling before method first it is calling data provider first but the object used in #dataprovider was initialized in the #beforemethod, As the dataprovider calling first for test2 it is throwing Null pointer exception. Is there any way to call the ‘#beforemethod’ before #Dataprovider.