JUnit - Use method in test from another class - java

The scenario is the following:
I have a class that executes tests for a section of a project.
public class section1 extends BaseTest {
#Test
public void test1() {
//somecode
}
}
The thing is that in test1, I want to use a Method that's defined in another test class (we're not using Page Objects in the automation project).
public class section2 extends BaseTest {
#Test
public void test2() {
//somecode
methodOne();
}
void methodOne(){
//somecode
}
}
So, I've tried creating an instance and access to the method through it
section2 sec2 = new section2();
.
.
.
#Test
public void test1() {
//somecode
section2.methodOne();
}
But it throws a NullPointerException as soon as it starts executing the method.
Then, I tried just extending the class section1 to section2 and use the method, so here it executes the method but, as soon as test1 finishes, it starts executing the tests from the extended class, in this case, test2.
How can I do to prevent that, or is there another way to use the method without extending and not making the method public?

Section 1 extends BaseTest
Section 2 extends BaseTest
Why don't you try this :
Section 2 extends Section 1

Add the below code in the section1 class:
section2 sec2;
#Before
public void initialize() {
sec2 = new section2();
}
As we are using the #Before JUnit annotation above, this 'initialize()' method will get executed first there by initializing the 'section2' class.
#Test
public void test1() {
sec2.methodOne();
}
Then in the #Test JUnit method will get executed so you won't get NPE.

Related

How to instantiate objects before testNG class?

I have multiple testNG test classes that extend a Base Test class and all use the same common objects. I want to have the object creation done automatically in BaseTest so I don't have to include it in each test class. As of now, the code only works if I add createPages() to the start of the test. I tried putting them in the BaseTest class using #BeforeClass and #BeforeSuite but both gave a null pointer exception meaning they weren't instantiated before the #Test test123 was run I beleive.
public someTest extends BaseTest {
#Test
public void test123(){
createPages(); //i want to be able to remove this and have it done in BaseTest
menuPage.scroll();
}
}
public BaseTest {
MenuPage menuPage;
public void createPages() {
menuPage = new MenuPage(getDriver());
}
/*
#BeforeSuite
public void beforeSuite() {
createPages();
}
#BeforeClass
public void beforeClass() {
createPages();
}
*/
}
#beforeTest is one such annotation. A method with #beforeTest annotation will run, before any test method belonging to the classes inside the test tag
inside ur BaseTest
public class Basetest{
#BeforeTest
public void doBeforeTest() {
createPages();
}
}

How to execute a piece of code once before all test classes start?

How can I execute a method once before all tests in all classes start?
I have a program that needs a system property to be set before any test start. Is there any way to do that?
Note: #BeforeClass or #Before are used just for the same test class. In my case, I'm looking for a way to execute a method before all test classes start.
To setup precondition to your test cases you can use something like this -
#Before
public void setUp(){
// Set up you preconditions here
// This piece of code will be executed before any of the test case execute
}
if you need to run that method before the start of all test you should use the annotation #BeforeClass or if you need to execute the same method every time you will execute a test method of that class you must use #Before
f.e
#Before
public void executedBeforeEach() {
//this method will execute before every single test
}
#Test
public void EmptyCollection() {
assertTrue(testList.isEmpty());
}
You can make use of a Test Suite.
The test suite
#RunWith(Suite.class)
#Suite.SuiteClasses({ TestClass.class, Test2Class.class, })
public class TestSuite {
#BeforeClass
public static void setup() {
// the setup
}
}
and, the test classes
public class Test2Class {
#Test
public void test2() {
// some test
}
}
public class TestClass {
#Test
public void test() {
// some test
}
}
Or, you can have a base class which handles the setup
public class TestBase {
#BeforeClass
public static void setup() {
// setup
}
}
and, then the test classes can extend the base class
public class TestClass extends TestBase {
#Test
public void test() {
// some test
}
}
public class Test2Class extends TestBase {
#Test
public void test() {
// some test
}
}
However, this will call the setup method in TestBase for all its subclasses everytime each of them executes.

Why doesn't my #BeforeClass method run when executing a subclass test method?

I'm using IntelliJ IDEA CE 2018.3 and JUnit 4.12.
I have a test class that looks like this:
#RunWith(HierarchicalContextRunner.class)
public class TestClass {
#BeforeClass
public static void beforeAll() {
//start a server for all tests to hit
}
#Before
public void before() {
//init a common request object for each test
}
#Test
public void itShouldHaveSomeCommonProperty() {
//check some common thing
}
public class SomeSubTestClass {
#Before
public void before() {
//do some test case-specific setup
}
public class SomeOtherSubTestClass {
#Test
public void itShouldDoSomething() {
//hit the service and assert something about the result
}
}
}
}
When I tell IntelliJ to run the class, everything works as expected. However, if I want to just run the itShouldDoSomething test (which I'm doing by setting up a run configuration that targets the SomeOtherSubTestClass class), the beforeAll method is not executed. Both of the before methods are executed in the correct order, but not the static beforeAll method.
Am I misunderstanding something, or is this a bug?
It is not a bug.
The beforeAll method is static and therefore tied to the class and not the instance. This is why it is not executed when calling tests in inner classes or sub-classes.
To ensure it being called you would have to define a #BeforeClass method in each of your inner classes which then call the method on the outer class.

How can I control whether JUnit creates an instance of the class whose behaviour is being tested?

I have noticed that JUnit implicitly creates an instance of my test class. I have added my own call to the constructor and this does not prevent the creation of the instance by JUnit; the net result is two instances are created, as shown by the console output below.
I find this puzzling. Why is this taking place, and how can I control/prevent the creation of the instance by JUnit? A google search "junit implicit object creation" reveals nothing, but I was able to see where the constructor is invoked by debugging the test. What I don't understand is why this is taking place, when we have a place to do it ourselves, and how to prevent it from taking place. I am using JUnit 4 in eclipse photon.
Thanks.
public class MainTest extends Main {
static Main m;
#BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("setUpBeforeClass");
m = new Main();
}
#AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("tearDownAfterClass");
}
#Before
public void setUp() throws Exception {
System.out.println("setup");
}
#After
public void tearDown() throws Exception {
System.out.println("tearDown");
}
#Test
public void testAdd() {
assertEquals(8,m.add(3,5));
}
}
Console output:
setUpBeforeClass
Main()
Main()
setup
tearDown
tearDownAfterClass
Your testcase extends the class Main which means the constructor is called upon creation of both the MainTest class and the explicit call of new Main()
remove the extends Main and you'll be good

TestNg's #BeforeTest on base class only happening once per fixture

I'm trying to use #BeforeTest to get code to ... run once before every test.
This is my code:
public class TestBase {
#BeforeTest
public void before() {
System.out.println("BeforeTest");
}
}
public class TestClass extends TestBase{
#Test
public void test1(){}
#Test
public void test2(){}
}
"BeforeTest" is only printed once, not twice. What am I doing wrong?
Use #BeforeMethod, not #BeforeTest.
The meaning of #BeforeTest is explained in the documentation.
"BeforeTest" is only printed once, not twice. What am I doing wrong?
***Sorry. I haven't noticed that you is written #BeforeTest , but in your example #BeforeTest almost equals #BeforeClass , and better to use #BeforeClass , when you haven't anymore test classes.
#BeforeClass" should be declared in same class that your tests methods, not differently!
//Example
package test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class Tests {
private String bClass;
private String bMethod1;
private String bMethod2;
#BeforeClass
public void beforeClass() {
bClass = "BeforeClass was executed once for this class";
}
#BeforeMethod
public void beforeMetodTest1() {
bMethod1 = "It's before method for test1";
}
#Test
public void test1() {
System.out.println(bClass);
System.out.println(bMethod1);
}
#BeforeMethod
public void beforeMethodTest2() {
bMethod2 = "It's before method for test2";
}
#Test
public void test2() {
System.out.println(bClass);
System.out.println(bMethod2);
}
}
#BeforeClass will executed once, before your all tests methods in this class. #BeforeMethod will executed before test method, before which it is written.
#BeforeClass may be only one in test class, in difference #BeforeMethod!(If it is some #BeforeClass, they are carried out by turns, but it not a correct composition of the test)
P.S. Sorry for my English :)
According to documentation, a method annotated with #BeforeTest is run before any #Test method belonging to the classes inside the tag is run.
From my experience:
Each #BeforeTest method is run only once
If you have several #BeforeTest methods, the order of their execution depends on the order of the class containing those #BeforeTest method.
You could test this by setting up a simple example.
If you use #beforeTest, that method will be run once in the beginning of every <test> (we specify in the test suit xml file) if that test contains that class
All the #befortests within all the classes within a <test> will be executed at the beggining of that test
Official Documentations are so unclear and mostly ambiguous. This is why many don't like to read it. Not to mention no real-life examples shown.
If you are trying to get code to run once before every test, then you are looking for #BeforeMethod, not #BeforeTest. Because each of your tests independently considered methods.
#BeforeMethod runs before each method. I see you have two methods with the names test1() and test2(). Expect BeforeMethod to run before each of them.
#BeforeTest runs only once before ALL of your tests (test-methods). In Selenium, WebDriver is called once, this is better practice for testing.
#BeforeMethod will invoke WebDriver before each test, this is not good practice, especially when you have regression tests with hundreds of them to run.
For example, if you run this code (these are separate classes, not inner to each other, only showing for demonstration purposes here):
public class TestClass extends Base {
#Test
public void test1() {
System.out.println("Running Test 1");
}
#Test
public void test2() {
System.out.println("Running Test 2");
}
}
public class Base {
#BeforeTest
public void beforeTest() {
System.out.println("Before Test");
}
#AfterTest
public void afterTest() {
System.out.println("After Test");
}
#BeforeMethod
public void beforeMethod() {
System.out.println("Before Method");
}
#AfterMethod
public void afterMethod() {
System.out.println("After Method");
}
}
And you will get this as an output:
Before Test
Before Method
Running Test 1
After Method
Before Method
Running Test 2
After Method
After Test
As you can see BeforeMethods run before each test while BeforeTest and AfterTest run only once before and after completing ALL tests.
Hope this clarified the difference for you.

Categories

Resources