Mocking only selected methods inside a class

When you are writing test cases, there might be a case that you want to mock only a selected method and other methods of the class should run the real implementation. This is specially useful when the method you are testing calls another methods in the same class.You can use following way to achieve that task.

Class A{
methodA();
methodB();
methodC();
}

We need to test methodA() and it calls methodB() inside. We need to mock methodB().

ClassATest extends TestCase{

testMethodA()
  {
    A a = new A();
    spy = Mockito.spy(a);
    Mockito.doReturn().when(spy).methodB();
    a.methodA();
  }

}

Load Testing SQLServer with JMeter

SQL Server

 Microsoft SQL Server is a relational database management system developed by Microsoft. As a database, it is a software product whose primary function is to store and retrieve data as requested by other software applications. Different editions of Microsoft SQL Server are available. Performance of database system depend on several parameters such as operation mode (READ/WRITE/UPDATE), Number of records in DB and size of a single entry. 


Performance metrics 

Transaction Response Time
Transaction Response Time represents the time taken for the database to complete a defined transaction or business process. The importance of Transaction Response Time is that it gives an idea of how the application is performing in the measurement of time.The objective of a performance test is to ensure that the application is working perfectly under load.  

 Transaction Throughput 
 The speed of a database system is measured by the transaction throughput, expressed as a number of transactions per second. Generally, the speed of a database system is measured by the transaction throughput, expressed as a number of transactions per second. The two gating factors for database performance in a transactional system are usually the underlying database files and the log file. Both are factors because they require disk I/O, which is slow relative to other system resources such as CPU. 

JMeter
The Apache JMeter desktop application is open source, Java application designed to load test functional behavior and measure performance. It was originally designed for testing Web Applications but has since expanded to other test functions.  It can be used to simulate a heavy load on a server, group of servers, network or object to test its strength or to analyze overall performance under different load types. 

 JMeter Setup

1. To connect to SQL server database, you need to place the required JDBC driver (sqljdbc4.jar) under "JMeter/lib" directory.  you can download it from following link.
http://msdn.microsoft.com/en-us/sqlserver/aa937724

2. Run jmeter.bat in bin folder and you will get a window as below.


3. Test Plan--> Add--> Threads (users)
    Here, we are specifying the number of users. If we want to simulate 20 users, we can set the value for Number of Threads (users) as 20.



4. Thread Group--> Add--> Sampler--> JDBC Request
    Here, we define the sql query for the load test. Specify a value for Variable name field.



5. Thread Group--> Add--> Config Element--> JDBC Connection Configuration.

1. Variable Name:  variable name specified at Variable name field in JDBC Request elements
2. Maximum Number of Connecitons: 0 
3. Pool Timeout: 10000
4. Idle cleanup interval: 60000
5. Auto Commit: True
6. Transaction Isolation: Default
7. Keep-Alive: True
8. Maximum Connection Age: 5000
9. Validation query: Select 1
10. DatabaseURL: jdbc:sqlserver://:;databaseName=
11. JDBC Driver Class: com.microsoft.sqlserver.jdbc.SQLServerDriver
12. User Name: (SQL server access user name) 
13. Password: (user's password)



6. Thread Group--> Add--> Listener -->Graph Results
To view the performance test results, we can add various listeners.

Then we can run the Test.

Example graphs.

A network-related or instance-specific error occurred while establishing a connection to SQL Server- A possible fix

Some times when working with sql server you may get following error.

TITLE: Connect to Server
------------------------------

Cannot connect to

------------------------------
ADDITIONAL INFORMATION:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 2)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&EvtSrc=MSSQLServer&EvtID=2&LinkId=20476

------------------------------
BUTTONS:

OK
------------------------------


A possible fix this is given below.

Go to services and check SQL Server (MSSQLSERVER) service. It may be stopped. Start it and then try to connect again.

XMLDecoder returns null pointer exception


Last week, I had to face following scenario.
My application is a client server application. An object of type class A, is encoded using java.beans.XMLEncoder in client side and saved in database. Class A is located in client side. I need to decode that object at server side. When I try to use java.beans.XMLDecoder,xmlDecoder.readObject() method returns null.

So I investigated why it is happening.  It was because class A is not available at server side, when XMLDecorder try to un marshall the object. 


How to fix "0 rows affected issue" in SQL UPDATE

I was trying to update some rows of a table using sql update statement, but it always returned "0 rows affected " message. I was using following syntax for that.

UPDATE table_name
SET column1=value1,column2=value2,...
WHERE column1='AB1 2CD';


The issue was the space inserted within the value I compare in where statement. So I used a column value, which has no spaces within its value for where statement. Then it worked.


You can get more information about this issue from following link.


http://stackoverflow.com/questions/14511018/where-clause-doesnt-work-in-sql-because-of-space-character

How to Debug a Web Service running in Tomcat

Debugging a Web service running in tomcat is really useful some times. Following are the necessary steps for that.

1) Add following line to startup.bat file within  \bin location.

set CATALINA_OPTS=-Xdebug -Xnoagent -DJava.compiler=NONE Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000


2) Create a new eclipse debug configuration of type Remote Java Application for the project  and set its connection properties as below.
     Host: localhost
     Port:8000

3) Start tomcat and run the debug configuration.

Now, you can debug your web service.

SQL Exception: Transaction is not active: tx=TransactionImpl

I got this exception last week when I was writing a task service to perform a task that involves retrieving data from a database. I found the SQL statements that it try to run from the The log files. They worked fine and gave correct results when I run it from SQL Server Management Studio.Then I googled the exception and found following details are helpful. Most of the time this exception occur because of a timeout.

You can fix it by increasing the value of transactiontimeout property in conf/jboss-server.xml. more info: http://ttlnews.blogspot.com/2012/04/jboss-exception-transaction-is-not.html

 Another possible scenario is that there is a Null Pointer Exception in the flaw of your application. This caused the transaction to end and re-enter a loop when the transaction was not active. more info: http://thejavablog.wordpress.com/2010/04/05/transaction-is-not-active-txtransactionimple-ac-basicaction