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
1,001 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 13, 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,150 points
12,616 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 22, 2019 in Selenium by Abha
• 28,140 points
1,211 views
0 votes
1 answer
0 votes
2 answers

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,500 points
13,676 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,144 views
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,910 points
7,221 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,875 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 12, 2019 in Selenium by Anvi
• 14,150 points
6,554 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
• 28,140 points
7,664 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP