Junit 4 is not picking up my #Before & #After methods - java

My #Before and #After methods are not picking up by Junit
public class TestSetup {
#Before
public void browserSetUp() {
// code for before a test
}
#After
public void tearDown() {
// code after a test
}
}
In Another class file I have defined
public class Steps{
#Step
public void step1() {
//Code for step 1
}
#Step
public void step2() {
// Code for Step 2
}
}
Finally I am calling those steps for my Test
public class Tests {
Steps step = new Steps();
#Test
public void TC_0001 {
step.step1();
step.step2();
}
}
#Test method are getting executed but the #Before and #After methods are not executing before #Test method.Do I have to include the TestSetup class to somewhere ? Any help will be appreciated.
**Thought 1: As I am using Maven to build, my #Before #After methods resides in a class (TestSetup.java - Name is not ending with *Test.java and may be thats why Maven is not picking it up for execution?

#Before and #After are used in the same class that your test is running. You should put this methods on your test class:
public class Tests {
Steps step = new Steps();
#Test
public void TC_0001 {
step.step1();
step.step2();
}
#Before
public void browserSetUp() {
// code for before a test
}
#After
public void tearDown() {
// code after a test
}
}

#Before and #After are only executed before a single test, if they are defined in the same class as the #Test. In your case, the TestSetup class contains no tests. So either you let Test inherit from TestSetup or you create a rule that is executed "around" your test.

Related

How to launch application after each test Junit5 TestFX

I'm writing an application test with Junit5 and TestFX.
My intention is that the main test class relaunches the application after each test. As far as I know, I shall use the annotation #BeforeEach, and it didn't work for me.
Here is my test class:
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MainTest extends ApplicationTest implements FxRobotInterface {
Logger loggerGuiTesting = LoggerManager.getInstance().getLogger(LoggerType.GUI_TESTING);
#BeforeEach
#Override
public void start(final Stage stage) {
StartMain.getInstance();
this.loggerGuiTesting.log(Level.INFO, "Application starts!");
}
#AfterAll
public void endApplication() {
new ExitGuiTest().run(); // That's my internal test framework
}
#Test
public void atestIfOpeningScreenIsThere() {
verifyThat("#imageViewSplashScreenLogo", NodeMatchers.isNotNull());
verifyThat("#progressBarSplashScreen", NodeMatchers.isNotNull());
verifyThat("#labelSplashScreenVersion", NodeMatchers.isNotNull());
verifyThat("#labelSplashScreenDate", NodeMatchers.isNotNull());
this.loggerGuiTesting.log(Level.INFO, "testIfOpeningScreenIsThere, succeeded!");
}
#Test
public void btestIfRadioButtonOneExist() {
assertThat("#sourcesOneRadioButton", is("#sourcesOneRadioButton"));
this.loggerGuiTesting.log(Level.INFO, "testIfRadioButtonOneExist, succeeded!");
}
#Test
public cnextTest() {
new StartAnotherGuiTest().run();
this.loggerGuiTesting.log(Level.INFO, "anotherTest, succeeded!");
}
}
The question is: how can I relaunch the application after each test?
It is difficult to answer without taking a look at the StartMain class. It looks like you are using a singleton pattern there. If thats the case I would create a new method in StartMain that sets the singleton instance to null so when getInstance is called again, it has to be re-created:
#After //This should be executed after each test
public void destroyApp()
{
StartMain.getInstance().destroy();
}

TestNG - How to call individual Test classes in MultipleTest Class

I am starting to use selenium webdriver with TestNG. I have created a multiple-test class which can run multiple tests, however, how do I call the other classes in each test block without copying the whole code over?
public WebDriver driver;
//Test 1
#Test(priority = 0) //Set Priority of Test - Priority of test always starts from Zero
public void one() {
System.out.println("This is Test Case 1");
}
//Test 2
#Test(priority = 1) // Test priority 1
public void Two(){
System.out.println("This is Test Case 2");
}
Would I need to create a function to call in each test block to run the other classes?
Use the setUp() method as described here to instantiate that class and keep it as an attribute.
The method setUp() will be invoked after the test class has been built
and before any test method is run.
import org.testng.annotations.*;
public class MyTest {
private MyService myService;
#BeforeClass
public void setUp() {
myService = new MyService();
}
#Test
public void testSomething() {
myService.doSomething();
}
}

Execute tests sequentially using Arquillian TestRunner TestNG

I can use Arquillian TestRunner JUnit Container to write sequential tests.
import org.jboss.arquillian.junit.InSequence;
import org.jboss.arquillian.junit.Arquillian;
#RunWith(Arquillian.class)
public class ClassToTest{
#Test
#InSequence(1)
public void test1() {
// test something (1)
}
#Test
#InSequence(2)
public void test2() {
// test something (2)
}
}
It is possible to do same thing using Arquillian TestRunner TestNG Container? If so how can I do that.
Yes. You can do the sequencing of test methods by dependency chaining in TestNG.
it would be like the below
#Test
public void test1() {
// test something (1)
}
#Test(dependsOnMethods = { "test1" })
public void test2() {
// test something (2)
}
Please refer the below for more info
http://www.tutorialspoint.com/testng/testng_dependency_test.htm

#BeforeClass annotation is not working in dynamically created Test Suite

I have few JUnit Tests and I want to decide which one to use at runtime. I checked previous answers at SO and I ended up creating Test Suite dynamically.
This class is where my application starts. I have CustomTestSuite class and Main class adds Tests to my custom suite.
public class Main {
public static junit.framework.TestSuite suite()
{
CustomTestSuite suite = new CustomTestSuite();
suite.addTest(new JUnit4TestAdapter(BTest.class));
suite.addTest(new JUnit4TestAdapter(ATest.class));
return suite;
}
}
CustomTestSuite.java
public class CustomTestSuite extends TestSuite {
#BeforeClass
public static void setUp() throws Exception {
System.out.println("Before class test");
}
#After
public void tearDown() throws Exception {
System.out.println("After class test");
}
}
My ATest and BTest are simple Test classes, I will just show ATest as sample:
public class ATest{
#Test
public void testMethod() {
System.out.println("testMethod");
}
}
When I start running my project from Main class, it is expected to run the method with #BeforeClass first, do testing, and then run the method with #AfterClass annotation.
Tests are working fine but it skips setUp method and tearDown method. I tried #Before and #BeforeClass annotations both.
I am confused with suite structure. Any help would be appreciated.
Thanks
#Before and #BeforeClass are supposed to be used in Test class not in TestSuite. If need to have common setUp and tearDown for more than one Test class, then put those both methods in a super class and extend that super by ATest and BTest test classes. And also the Suite can be built and run simply with #RunWith and #SuiteClasses annotations and the CustomTestSuite class is not needed.
So the changes are as below.
The CustomTestSuite becomes TestSuper
public class TestSuper {
#BeforeClass
public static void setUp() throws Exception {
System.out.println("Before class test");
}
#After
public void tearDown() throws Exception {
System.out.println("After class test");
}
}
Now the ATest extends TestSuper
public class ATest extends TestSuper {
#Test
public void testMethod() {
System.out.println("testMethod");
}
}
Similarly BTest also should extend TestSuper.
Simply add #RunWith and #SuiteClasses annotations to Main class as below and run Main.
#RunWith(Suite.class)
#SuiteClasses({ATest.class, BTest.class})
public class Main {
}
Have a go with these changes.

Why isn't my #BeforeClass method running?

I have the following code:
#BeforeClass
public static void setUpOnce() throws InterruptedException {
fail("LOL");
}
And various other methods that are either #Before, #After, #Test or #AfterClass methods.
The test doesn't fail on start up as it seems it should. Can someone help me please?
I have JUnit 4.5
The method is failing in an immediate call to setUp() which is annotated as #before.
Class def is :
public class myTests extends TestCase {
do NOT extend TestCase AND use annotations at the same time!
If you need to create a test suite with annotations, use the RunWith annotation like:
#RunWith(Suite.class)
#Suite.SuiteClasses({ MyTests.class, OtherTest.class })
public class AllTests {
// empty
}
public class MyTests { // no extends here
#BeforeClass
public static void setUpOnce() throws InterruptedException {
...
#Test
...
(by convention: class names with uppercase letter)
the method must be static and not directly call fail (otherwise the other methods won't be executed).
The following class shows all the standard JUnit 4 method types:
public class Sample {
#BeforeClass
public static void beforeClass() {
System.out.println("#BeforeClass");
}
#Before
public void before() {
System.out.println("#Before");
}
#Test
public void test() {
System.out.println("#Test");
}
#After
public void after() {
System.out.println("#After");
}
#AfterClass
public static void afterClass() {
System.out.println("#AfterClass");
}
}
and the ouput is (not surprisingly):
#BeforeClass
#Before
#Test
#After
#AfterClass
Make sure you imported #Test from the correct package.
Correct package: org.junit.Test
Incorrect pacakge: org.junit.jupiter.api.Test
Please note that this is a solution for: If your #Before, #Atter, etc did not get called at all.
Make sure that :
Your test class doesn't inherits from TestCase
The #BeforeClass method is static
You don't have more than one #BeforeClass method in test class hierarchy (only the most specialized #BeforeClass method will be executed)
Check your imports.
#Before
#After
#BeforeClass (this should be static)
#AfterClass (this should be static)
and #Test annotations should import from same path.
In order that the before annotated function will run , I had to do the following:
If you use Maven , add a dependency to Junit 4.11+:
<properties>
<version.java>1.7</version.java>
<version.log4j>1.2.13</version.log4j>
<version.mockito>1.9.0</version.mockito>
<version.power-mockito>1.4.12</version.power-mockito>
<version.junit>4.11</version.junit>
<version.power-mockito>1.4.12</version.power-mockito>
</properties>
and the dependency:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>
.
.
.
</dependencies>
Make sure your Junit Test class is not extending The TestCase class, since this will cause overlapping with Older version:
public class TuxedoExceptionMapperTest{
protected TuxedoExceptionMapper subject;
#Before
public void before() throws Exception {
subject = TuxedoExceptionMapper.getInstance();
System.out.println("Start");
MockitoAnnotations.initMocks(this);
}
}

Categories