JUnit Rule not being executed when test fails - junit-rule

Following is my test file :
package test;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
public class TestWatcherTest
{
#Rule
public TestWatcher testWatcher = new TestWatcher()
{
protected void failed(Throwable e, Description description)
{
System.out.println("in failed()");
}
};
#Test
public void shouldFail()
{
Assert.assertTrue("Test failed", false);
}
}
Output is :
<failure message="Test failed" type="junit.framework.AssertionFailedError">junit.framework.AssertionFailedError: Test failed at test.TestWatcherTest.shouldFail(TestWatcherTest.java:24)
</failure>
It seems failed() is not being executed. I read many posts but nothing seems to work for me. What am I doing wrong?

The issue was with ant configuration. From http://testautomationusingjunit.blogspot.com/2013/08/applying-rules-to-junit-tests-running.html, the target needs to be edited to contain JUnitCore in the classpath.
<target name="junit" depends="build">
<java classname="org.junit.runner.JUnitCore">
<classpath>
<path location="selenium_server/selenium-server-standalone-xxx.xx.jar"/>
</classpath>
</java>
<junit fork="no" haltonfailure="no" printsummary="true" failureProperty="test.failed" dir=".">
<test name="src.SampleTest" todir="./report" />
</junit>
</target>

Related

but has not been marked #Optional in testng.xml

I am trying to run the below code using TestNG,
package Framework;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
public class XMLtestng {
WebDriver d;
#Test
#Parameters({"fname"})
public void reg(String fname) throws InterruptedException {
d.findElement(By.linkText("REGISTER")).click();
d.findElement(By.name("firstName")).sendKeys(fname);
d.findElement(By.name("register")).click();
Thread.sleep(3000);
}
#BeforeClass
public void beforeClass() {
d = new FirefoxDriver();
d.manage().window().maximize();
d.get("http://newtours.demoaut.com/");
}
#AfterClass
public void afterClass() {
d.close();
}
}
And this is the corresponding xml file,
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="reg">
<Parameter name="fname" value="Rachel"/>
<classes>
<class name="Framework.XMLtestng"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
when I run my xml file as TestNg suite,only the website's url is being opened and close. Actions to be performed (typing the firstname for registration), is not being executed. This is the message being printed in the console,
Suite
Total tests run: 1, Failures: 0, Skips: 1
===============================================
Can anybody please help me in resolving this.
Thanks in advance.
Your test is skipped because you didn't provide it the expected parameter and it is not declared as #Optionnal.
The problem is you used <Parameter... instead of <parameter....
Use the DTD to validate your file:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

Izpack can not find class of custom action defined for InstallPanel

I use 5.0.0-rc4 as izpack version and izpack-installer artifact exists as a dependency in my pom.xml.
<dependency>
<groupId>org.codehaus.izpack</groupId>
<artifactId>izpack-installer</artifactId>
<version>${izpack.version}</version>
</dependency>
I have defined a custom action DeletePreviousInstallationAction for InstallPanel.
InstallPanel definition is included in install.xml as below.
<panels>
<panel classname="TargetPanel"/>
<panel classname="UserInputPanel" id="panelUserInput"/>
<panel classname="InstallPanel">
<actions>
<action stage="preconstruct" classname="com.x.y.z.w.DeletePreviousInstallationAction" />
</actions>
</panel>
<panel classname="ProcessPanel"/>
<panel classname="SimpleFinishPanel"/>
</panels>
DeletePreviousInstallationAction code:
package com.x.y.z.w;
import com.izforge.izpack.api.data.InstallData;
import com.izforge.izpack.api.data.PanelActionConfiguration;
import com.izforge.izpack.api.handler.AbstractUIHandler;
import com.izforge.izpack.data.PanelAction;
public class DeletePreviousInstallationAction implements PanelAction {
#Override
public void executeAction(InstallData id, AbstractUIHandler auih) {
System.out.println("Intall path: " + id.getInstallPath());
}
#Override
public void initialize(PanelActionConfiguration pac) {
}
}
When I try to build setup project, I get Failure: Class 'com.x.y.z.w.DeletePreviousInstallationAction' not found. Why does this happen?
You are most likely missing it during compilation, it has to be added in "jar" section to install xml of izpack e.g.:
<jar src="#{jmx4ant:jmx4ant:jar}" stage="both" />

TestNG configuration failure

I am trying to run a simple TestNG test from the command line, it errors with a configuration failure. The same TestNG test will run from Eclipse IDE correctly. From command line it does not work. This is a severe limitation of this TestNG framework making it not very useable in a Continuous integration theme. If you plan on working with TestNG ensure you can get it running from the command line before committing to using it as your testing framework. Having to run it from Eclipse IDE is a severe limitation.
TestSuite_Bollosk
Total tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1
command line syntax is:
java -cp "C:\correctclpath1\*;C:\correctclpath2\*" org.testng.TestNG "C:\anotherpath\TS_simpletest.xml"
the test looks like this:
package pkgTSBollosk;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TC_Bollosk
{
WebDriver driver;
#Parameters({"param1","param2"})
#Test
public void bollosktestmethod(String param1, String param2) throws InterruptedException
{
assert(true);
System.out.println("test output for bollosk test:");
}
#BeforeTest
public void beforeTest() throws IOException {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(2,TimeUnit.SECONDS);
}
#AfterMethod
public void afterTest() {
driver.quit();
driver = null;
}
}
the TS_simpletest.xml file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Bollosk Test Suit" parallel="false">
<test name="Bolluks test name">
<parameter name="Param1" value="not - used"></parameter>
<parameter name="Param2" value="not - used"></parameter>
<classes>
<class name="pkgTSBollosk.TC_Bollosk">
<methods>
<include name = "bollosktestmethod"></include>
</methods>
</class>
</classes>
</test>
</suite>
Substituting Assert.assertTrue for assert and importing org.testNG.assert fixed the problem. A trap for the newbie..... I retract my previous statement about severe limitation.
public class test1 {
public WebDriver d;
#Parameters({"browsername","url"})
#BeforeMethod
public void browserselections(String broname,String URL) {
System.out.println(broname);
System.out.println(URL);
if (broname.equalsIgnoreCase("ff")) {
d = new FirefoxDriver();
} else if (broname.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "F:\\All jars\\chromedriver.exe");
d = new ChromeDriver();
}
d.manage().window().maximize();
d.get(URL);
}
#Test
public void tc1() {
d.findElement(By.id("lst-ib")).sendKeys("testing");
}
}
below is my xml code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" configfailurepolicy="skip">
<test name="Test1">
<parameter name="browsername" value="ff"/>
<parameter name="URL" value="https://www.google.com"/>
<classes>
<class name="testng.test1" />
</classes>
</test>
<test name="Test2">
<parameter name="browsername" value="chrome"></parameter>
<classes>
<class name="testng.test1" />
</classes>
</test>
</suite> <!-- Suite -->enter code here
I was facing the same issue and i noticed that all of my jar files were in different sub folders. Move all of them in one library folder and execute with same command.
java -cp "%projectLocation%/Library/*";"%projectLocation%/target/classes/" org.testng.TestNG testng.xml
Try TestNG config policy. Add parameter configfailurepolicy="continue" if you wish to proceed with config failures else, configfailurepolicy="skip". Example
<suite name="Bollosk Test Suit" configfailurepolicy="continue">
If you remove driver.quit() from public void afterTest() method,
the Configuration Failures will not come.

With arquillian incontainer mode ,Why Test method is getting exeduted square of the times of values returned by data provider in testng?

I'm using TestNG as Unit Test Framework and Jboss AS7.1.1 Final as server
The data provider and Test methods works well in Client Mode
The same dataprovider will return 10 rows and my Test method is getting executed nearly 100times in In container mode
Test method
#Test(groups="bean-tests",dataProvider="Presenter-Data-Provider")
public void findByIdPositiveTest(long presenterId,String expectedPresenterName)
{
}
Dataprovider method:
#DataProvider(name = "Presenter-Data-Provider")
public Object[][] presenterTestDataProvider()
{
EntityManagerFactory emf=null;
EntityManager em=null;
Object testcaseData[][]=null;
Session session=null;
try
{
emf=Persistence.createEntityManagerFactory("TestCaseDataSource");
em=emf.createEntityManager();
session=em.unwrap(Session.class);
Criteria query=session.createCriteria(TestPresenter.class).setFirstResult(0).setMaxResults(10);
List<TestPresenter> rowList=query.list();
testcaseData=new Object[rowList.size()][2];
for(int loopCount=0;loopCount<rowList.size();loopCount++)
{
TestPresenter row=rowList.get(loopCount);
testcaseData[loopCount][0]=row.getPresenterId();
testcaseData[loopCount][1]=row.getExpectedPresenterName();
}
}
catch(Exception exception)
{
mLog.error(exception.getMessage());
}
return testcaseData;
}
I'm running as Test Suite using folowing Suite configuration
<test name="Bean testing">
<groups>
<run>
<!-- This has to be added by default while using arquillian Test Runner -->
<include name="arquillian" />
<include name="bean-tests" />
</run>
</groups>
<classes>
<class name="blah.blah.blah.PresenterManagerBeanTest" />
</classes>
</test>
Pls let me know What I did was wrong
Or direct me how to get values from DB to Data provider and tests using In container mode
Thanks in advance
sathiya seelan
It looks like it's related to https://issues.jboss.org/browse/ARQ-1282. Issue is still open.

Spring data repository #Autowiring is null when using #Transactional annotation on service bean

I am studying spring framework and trying to use it in my project. But I have came across the following problem with spring data repository and #Transactional annotation used in my service. The problem is that there are no exceptions on the spring start up. Later on when I try to access spring data repository I get NullPointerException. Maybe you have some thoughts that could help me.
I am using spring data repository define as following:
package net.question.data.repository;
import net.question.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
}
Then I have a service defined which contains autowired repository:
package net.question.data.service;
import net.question.data.repository.UserRepository;
import net.question.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
#Service
#Transactional
public class UserService {
#Autowired
public UserRepository userRepository;
public void doStuff(User usr) {
// login will be here
}
}
here is the test to show my problem:
package net.question.spring;
import static org.junit.Assert.assertNotNull;
import net.question.data.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration("/app-context.xml")
public class InjectionTestSuite {
#Autowired
UserService userService;
#Test
public void testRepositoryInjection() {
assertNotNull(userService);
assertNotNull(userService.userRepository);
}
}
The test fails on the follwowing line:
assertNotNull(userService.userRepository);
If I remove the #Transactional annotation on the service then the test passes.
here is my app-context.xml file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd ">
<jpa:repositories base-package="net.question.data.repository" />
<!-- For discovering entity services -->
<context:component-scan base-package="net.question.data.service" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="hibernate_mysql" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven />
</beans>
Maybe you have some ideas, how to find the error?