How to create a test class with a test method in JUnit to testing framework?

0 votes
How to create a test class with a test method in JUnit to testing framework?
Jun 12, 2019 in Selenium by Sargun
113 views

1 answer to this question.

0 votes

Hi Sargun, first we need to create a test class with a test method annotated with @Test as given below:

FirstClassTest.java

package testingDemo.JUnit;  

import static org.JUnit.Assert.*;
import org.JUnit.Test;
public class FirstClassTest {
    @Test
    public void FirstMethod(){
        String str= "JUnit is working fine";
        assertEquals("JUnit is working fine",str);
    }
}

To execute our above test method ,we need to create a test runner. In the test runner we have to add test class as a parameter in JUnitCore's runclasses() method . It will return the test result, based on whether the test is passed or failed:

package testingDemo.JUnit;		

import org.JUnit.runner.JUnitCore;		
import org.JUnit.runner.Result;		
import org.JUnit.runner.notification.Failure;		

public class TestRunner {				
			public static void main(String[] args) {									
            Result result = JUnitCore.runClasses(MyFirstClassTest.class);					
			for (Failure failure : result.getFailures()) {							
              System.out.println(failure.toString());					
      }		
      System.out.println("Result=="+result.wasSuccessful());							
   }		
}
answered Jun 12, 2019 by Nagendra

Related Questions In Selenium

0 votes
1 answer

How to create a Base Class in Selenium and use it with test scripts?

Hi Donna, you can use following code ...READ MORE

answered Jul 18, 2019 in Selenium by Anvi
• 14,050 points
2,552 views
0 votes
1 answer

How to create a test case with TestNG in Selenium?

Hey Deepak, to create a Test Case ...READ MORE

answered May 21, 2019 in Selenium by Abha
• 27,910 points
175 views
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,510 points
2,911 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
2,242 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,890 points
2,376 views
0 votes
1 answer

How to add Junit library in Eclipse to use with Selenium Webdriver?

Hi Jacky, to add JUnit library in ...READ MORE

answered Jun 11, 2019 in Selenium by Anvi
• 14,050 points
122 views
0 votes
1 answer

How to pass parameter through testng.xml file to a test case in Selenium Webdriver?

Hello Revathi, to pass parameter through testng.xml ...READ MORE

answered Jun 12, 2019 in Selenium by Abha
• 27,910 points
2,372 views