<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Technotrance, Illusions and Perspectives &#187; testng</title>
	<atom:link href="http://myblog.shriharisc.com/tag/testng/feed/" rel="self" type="application/rss+xml" />
	<link>http://myblog.shriharisc.com</link>
	<description>A dose of everyday bruises with Java/JEE</description>
	<lastBuildDate>Mon, 02 Aug 2010 17:45:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Understanding Exception Handling on Mock objects</title>
		<link>http://myblog.shriharisc.com/2010/06/06/understanding-exception-handling-on-mock-objects/</link>
		<comments>http://myblog.shriharisc.com/2010/06/06/understanding-exception-handling-on-mock-objects/#comments</comments>
		<pubDate>Sun, 06 Jun 2010 15:40:46 +0000</pubDate>
		<dc:creator>Shrihari</dc:creator>
				<category><![CDATA[jee-light]]></category>
		<category><![CDATA[mockito]]></category>
		<category><![CDATA[testng]]></category>

		<guid isPermaLink="false">http://myblog.shriharisc.com/?p=258</guid>
		<description><![CDATA[Mockito is the recommended and used mocking testing framework which provides varied function points to mock and verify interactions. It provides a parallel support to TestNG or JUnit for faster Test Driven Development (TDD) adoption in newer teams. In this edition of the write-up I would explore the concept of mocking with exception handling.
Lets consider [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://mockito.org/" target="_blank"><strong>Mockito</strong></a> is the recommended and used mocking testing framework which provides varied function points to mock and verify interactions. It provides a parallel support to<a href="http://testng.org" target="_blank"><strong> TestNG</strong></a> or <strong><a href="http://junit.org" target="_blank">JUnit</a></strong> for faster Test Driven Development (TDD) adoption in newer teams. In this edition of the write-up I would explore the concept of mocking with exception handling.</p>
<p>Lets consider a user credential validation login, before user getting authenticated into the system. If the credential validation fails, the system is expected to throw an application exception. However if it passes, the actual authentication to the system should<br />
happen. With the familiarity with TestNG and Mockito, lets look the test case code</p>
<pre class="brush: java">
@Test(expectedException=UserValidationException.class)
public void testCredentialsValidation()
{  //create a mock to return false for validation - to fail login
UserCredentials credentials = mock(UserCredentials.class);
when(credentials.validate(anyString(),anyString())).thenReturn(false);

//Call the login service to gain entry
LoginService service = new LoginService(credentials);
service.loginuser(&quot;sample&quot;,&quot;sampl3&quot;);
}
</pre>
<p>Please note here that UserCredentials is only being mocked here, not the LoginService. The reason is to run the induced behaviour of the mocked UserCredentials on the actual LoginService functionality. A mocked object will behaviour only according to what has been induced.</p>
<p>Having looked at the test code, lets look at the actual logic implementation</p>
<pre class="brush: java">
public interface UserCredentials
{  public boolean validate(String username,String passwordtext);
}
</pre>
<pre class="brush: java">
public class LoginService
{  private UserCredentials credentials;

public LoginService(UserCredentials credentials)
{ this.credentials = credentials;
}

public void loginuser(String user, String passwd) throws UserValidationException
{   if(!credentials.validate(user,passwd))
throw new UserValidationException(&quot;Username or password invalid..&quot;);
//actual authentication process...
}
}
</pre>
<pre class="brush: java">
public UserValidationException extends RuntimeException
{  private String message;

public UserValidationException(String msg)
{   message = msg;
}
}
</pre>
<p>UserValidationException extends RuntimeException for brevity purposes, so that the test code signature is not affected. There is another methodology whereby we could induce behaviour of throwing an exception on the mocked object, even though the actual need not throw any exception.</p>
<pre class="brush: java">
doThrow(new UserValidation()).when(loginservice).loginuser(anyString(),anyString());
</pre>
<p>Unfortunately this is at the method level and not at the individual statement level. Am looking at options on mocking exceptions at the method  statement level for the mocked objects. Any thoughts!</p>
]]></content:encoded>
			<wfw:commentRss>http://myblog.shriharisc.com/2010/06/06/understanding-exception-handling-on-mock-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit testing CXF webservice endpoints using Spring-Test and TestNG</title>
		<link>http://myblog.shriharisc.com/2009/10/09/unit-testing-cxf-webservice-endpoints-using-spring-test-and-testng/</link>
		<comments>http://myblog.shriharisc.com/2009/10/09/unit-testing-cxf-webservice-endpoints-using-spring-test-and-testng/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 16:05:44 +0000</pubDate>
		<dc:creator>Shrihari</dc:creator>
				<category><![CDATA[jee-light]]></category>
		<category><![CDATA[apache-cxf]]></category>
		<category><![CDATA[spring-test]]></category>
		<category><![CDATA[testng]]></category>

		<guid isPermaLink="false">http://schakrap.wordpress.com/?p=127</guid>
		<description><![CDATA[The Spring IOC feature of configurational wiring of POJOs to control and achieve network of business logic frees developers of writing any boilerplate code and eventually of bugs. I would like to explain one such facility with regard to establishing an unit testing infrastructure for writing unit test cases for JAXWS specific web service endpoints [...]]]></description>
			<content:encoded><![CDATA[<p>The Spring IOC feature of configurational wiring of POJOs to control and achieve network of business logic frees developers of writing any boilerplate code and eventually of bugs. I would like to explain one such facility with regard to establishing an unit testing infrastructure for writing unit test cases for JAXWS specific web service endpoints (CXF) using Spring-test module and TestNG unit testing framework.</p>
<p>Lets take a simple example of a web service endpoint exposing a functionality to query a book price, given the ISBN number, given a maven project. This write-up just concentrates on establishing a web service unit testing framework. Following are sequence of steps one could follow to achieve the required infrastructure setup.</p>
<p>1) Change ${basedir}/pom.xml include adding spring-test dependency, and TestNG dependency, with maven-surefire-plugin configuration (assuming CXF dependencies are already present)</p>
<pre class="brush: xml">
&lt;dependency&gt;
 &lt;groupId&gt;org.springframework&lt;/groupId&gt;
 &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
 &lt;version&gt;2.5.6&lt;/version&gt;
 &lt;/dependency&gt;
&lt;dependency&gt;
 &lt;groupId&gt;org.testng&lt;/groupId&gt;
 &lt;artifactId&gt;testng&lt;/artifactId&gt;
 &lt;version&gt;5.9&lt;/version&gt;
 &lt;classfier&gt;jdk15&lt;/classifier&gt;
 &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
</pre>
<p>2) Add a Spring configuration file (service-test.xml) under ${basedir}/src/test/resources. The CXF&#8217;s JaxWSProxyFactoryBean can be wired to to create a service proxy. Below is the snippet of the configuration file.</p>
<pre class="brush: xml">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xmlns:cxf=&quot;http://cxf.apache.org/core&quot;
xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd&quot;&gt;&lt;/span&gt;&lt;/pre&gt;
    &lt;import resource=&quot;classpath:META-INF/cxf/cxf.xml&quot; /&gt;
     &lt;import resource=&quot;classpath:META-INF/cxf/cxf-extension-soap.xml&quot; /&gt;
     &lt;import resource=&quot;classpath:META-INF/cxf/cxf-extension-http.xml&quot; /&gt;

     &lt;!-- Required to load the service endpoint uris --&gt;
     &lt;bean id=&quot;propConfig&quot; class=&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;&gt;
         &lt;property name=&quot;locations&quot;&gt;
            &lt;list&gt;
               &lt;value&gt;classpath:service-test.properties&lt;/value&gt;
             &lt;/list&gt;
         &lt;/property&gt;
     &lt;/bean&gt;

     &lt;bean id=&quot;proxyFactory&quot; class=&quot;org.apache.cxf.jaxws.JaxWsProxyFactoryBean&quot;&gt;
         &lt;property name=&quot;serviceClass&quot; value=&quot;com.schtech.service.endpoint.store.BookPriceService&quot;/&gt;
         &lt;property name=&quot;address&quot; value=&quot;${bookstore.service.endpoint.url}&quot;/&gt;
         &lt;property name=&quot;bus&quot; ref=&quot;cxf&quot; /&gt;
    &lt;/bean&gt;

     &lt;bean id=&quot;serviceClient&quot; class=&quot;com.schtech.service.endpoint.store.BookPriceService&quot; factory-bean=&quot;proxyFactory&quot;
        factory-method=&quot;create&quot;/&gt;
&lt;/beans&gt;
</pre>
<p>3) Implement the TestNG test case to test web service. Here three things have to be noted: (1) ContextConfiguration annotation helps us load<br />
the configuration file. (2) The test case extends AbstractTestNGSpringContextTests to load the context configuration and autowire the service<br />
bean. (3) Autowire annotation on the service endpoint interface initializes with proxy factory created service proxy instance.</p>
<pre class="brush: java">
@ContextConfiguration(locations = {&quot;classpath:service-context.xml&quot;})
public class BookCatalogTest extends AbstractTestNGSpringContextTests
 { @Autowired
   private BookPriceService serviceClient;

   @Test
   public void testBookPrice()
   { String isbn = &quot;978-3-16-148410-0,&quot;;
     Double price  = serviceClient.findBookPrice(isbn);
     assertNotNull(price);
   }
}
</pre>
<p>The infrastructure is ready to be used to test other service endpoints hosted elsewhere. Running mvn test at the ${basedir} prompt would unit test the service endpoints.</p>
]]></content:encoded>
			<wfw:commentRss>http://myblog.shriharisc.com/2009/10/09/unit-testing-cxf-webservice-endpoints-using-spring-test-and-testng/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
