Showing posts with label unit test. Show all posts
Showing posts with label unit test. Show all posts

Thursday, August 9, 2012

Unit test; xUnit frameworks & developer testing tools

An unit is the smallest testable part of an application.
In OO programming a unit is often a class, but could be also quite often a specific method of a class. This applies also to Java programming language.
Unit tests often verify the API’s contract. This means that it check that the code does what is expected to do.
Junit framework offers the posibility to create automated unit tests. There are also other similar frameworks
Similar to JUnit framework, unit testing frameworks for other programming languages were developed. The name XUnit was given to the collection of all these frameworks.
According to Martin Fowler’s article on XUnit :
XUnit is the family name given to bunch of testing frameworks that have become widely known amongst software developers. The name is a derivation of JUnit the first of these to be widely known.

Unit testing frameworks/tools for the web:
Framework/Tool Description
HtmlUnit Java headless browser emulator
HttpUnit testing framework for web applications, typically used in combination with JUnit
IEUnit testing framework for web applications, based on IE browser and Javascript
Selenium Testing framework whose playback can run in most modern web browsers to test webpages.
Watir Testing framework based on the Ruby language, that can run in web browsers to test webpages.
soapUI Open source web service testing tool for service-oriented architectures (SOA).
SOAtest Parasoft SOAtest automates web application testing, message/protocol testing, cloud testing and security testing.

Java unit testing frameworks:
Framework/Tool Description
JUnit Unit testing framework for Java language.
TestNG An integration testing framework, which means its tests include unit tests, functional tests, and integration tests.
JExample A JUnit extension that uses dependencies between test cases to reduce code duplication and improves defect localization.
DbUnit A JUnit extension to perform unit testing with database-driven programs
JUnitEE A JUnit extension for testing Java EE applications
Cactus A JUnit extension for testing Java EE and web applications. Cactus tests are executed inside the Java EE/web container.
JSST Java Server-Side Testing framework which is based on the similar idea to the one of Apache CACTUS, but unlike CACTUS it’s not coupled to JUnit 3.x and can be used in conjunction with any testing framework.
GroboUtils A JUnit extension providing Automated documentation, class hierarchy unit testing, code coverage, and multi-threaded tests.
Mockrunner Mockrunner A JUnit extension for testing testing servlets, filters, tag classes and Struts actions and forms.
Unitils Offers general utilities and features for helping with persistence layer testing and testing with mock objects. Offers specific support for testing application code that makes use of JPA, hibernate and spring. Unitils integrates with the test frameworks JUnit and TestNG.
XMLUnit JUnit and NUnit testing for XML
JMock A mock framework
EasyMock A mock framework
Mockito A mock framework, using the Hamcrest library
PowerMock An extension to both Mockito and EasyMock that lets you test static methods
Concutest A framework for testing concurrent programs
JBehave Behavior Driven Development
Instinct Behavior Driven Development
JDave Behavior Driven Development
beanSpec Behavior Driven Development

JavaScript unit testing frameworks:
Framework/Tool Description
DOH Dojo Objective Harness that can be run in-browser or independently via Rhino
JSUnit Open source Unit Testing framework for client-side (in-browser) JavaScript. It is essentially a port of JUnit to JavaScript.
JSUnit is no longer actively maintained.
QUnit jQuery test harness
Crosscheck Browserless java-based framework
J3Unit An object-oriented unit testing framework for JavaScript.
J3Unit runs JavaScript tests directly in the web browser and can be automated using JUnit and Jetty.
J3Unit builds on previous work by JSUnit and Script.aculo.us to provide a better way to automate JavaScript unit tests.
YUI Test Testing framework developed by YAHOO for browser-based JavaScript solutions. Using YUI Test, you can easily add unit testing to your JavaScript solutions. While not a direct port from any specific xUnit framework, YUI Test does derive some characteristics from nUnit and JUnit./td>
JSSpec Behaviour-driven development framework
UnitTesting script.aculo.us javascript test harness
Jasmine BDD, framework independent, easy integration with Ruby projects and continuous builds. Allows for both DOM-less testing and asynchronous testing.
RhinoUnit Rhino-based framework that allows tests to be run in Ant
JasUnit Light-weight framework. Part of a project that provides Mocks and IoC.)
FireUnit Testing framework that provides logging and viewing within a new tab of Firebug
Js-test-driver The goal of JsTestDriver is to build a JavaScript test runner which easily integrates with continuous builds systems and allows running tests on multiple browsers quickly to ease TDD style development.
Js-test-runner A JUnit test runner that runs JS tests. The goal of the JS Test Runner is to be able to test JS code using an approach that test driven programmers will feel comfortable with; particularly in the context of Continuous Integration.
Nodeunit Asynchronous Javascript testing framework
Some other XUnit frameworks are:
Language Framework Description xunit
Adobe Flex & ActionScript FlexUnit 4 Metadata-driven unit testing for Flex 2,3 and 4 and ActionScript 3 projects yes
Adobe Flex & ActionScript FUnit Metadata-driven unit testing for Flex yes
ASP ASPUnit

BPEL BPELUnit

C AceUnit AceUnit is JUnit 4.x style, easy, modular and flexible. yes
C CUnit OS Independent yes
C++ CppUnit
yes
C++ CPUnit
yes
C++ Google C++ Testing Framework
yes
C++ Symbian OS Unit

CFML (ColdFusion) MXUnit Unit Test Framework and Eclipse Plugin for CFML (ColdFusion) yes
Groovy Spock

Haskell HUnit
yes
LabVIEW VI Tester Native LabVIEW object-oriented implementation of xUnit framework. Unit tests are written as VIs in LabVIEW. yes
MATLAB MATLAB xUnit Test Framework Accepts both xUnit-style subclass or simple MATLAB function test cases. yes
MySQL utMySQL Unit test framework for MySQL V 5 using stored procedures. yes
Perl TAP the Test Anything Protocol used by most Perl tests
Perl Test::Class xUnit style testing adapted to Perl yes
Oracle PL/SQL utPLSQL An open source unit testing framework for PL/SQL development modeled on the Junit and xUnit frameworks. yes
Oracle PL/SQL PL/SQL Unit Testing for Oracle (PLUTO) Open source unit testing framework modeled after JUnit and based on the Oracle PL/SQL object system yes
Prolog PlUnit

Python PyUnit it’s part of Python’s standard library yes
Ruby Test::Unit

Ruby RSpec Behaviour-driven development framework
Transact-SQL tSQLt Database unit testing framework for Microsoft SQL Server.
Includes mocking framework, continuous integration support and uses SQL Test as a GUI

XML XUnit For testing: native XML programs, individual XSLT templates, and Java programs that deal with XML data.
XML WUnit For testing Web applications.Tests are written in XML/XPath (XUnit).AJAX applications are supported, Can also test server-side functionalities if they are made with Java servlets (for example, it is possible to store authoritatively an object in the user session server-side without sending an HTTP request and then get with HTTP the page that renders it).
.NET xUnit.net Developed by the original inventor of NUnit to be its successor. xUnit.net is currently the highest rated .NET unit testing framework due to it being leaner with a more refined syntax and lower friction usage then NUnit. yes
.NET csUnit includes GUI, command line, VS2005 plug-in; supports C#, VB.NET, Managed C++, J#, other .NET languages, supports .NET 3.5 and earlier versions; integrated with ReSharper yes
.NET DbUnit.NET A .NET 2.0 unit testing framework for database access code

How do you unit test Java EE code?

If by Unit Testing you mean... unit testing (testing a unit in isolation), then you actually don't need any particular framework since EJB3.0 are nothing more than annotated POJOs and thus can be relatively easily tested without any special fixture.
Now, if you mean something else - like Integration Testing or Functional Testing - then, yes, tools can help and simplify things (but you should really start to use the right terminology :) I'll assume that this is what you have in mind.
First, JUnitEE seems dead and obsolete and I'm not even sure it has anything for EJB3.x. Second, I'm not impressed by the Java EE 5 support of Cactus and having to deploy Cactus tests is painful (I think that Cactus was nice for J2EE 1.4 but is a bit outdated now). So this leaves us with Ejb3Unit which is in my opinion the best option, especially if you want to run out of container tests i.e. without really deploying the application (much faster).
If you want to run in container tests, then you could indeed use an embedded container and my current preference goes to GlassFish v3, even for Java EE 5 (I may be wrong but I'm pretty disappointed by the starting time of the latest JBoss releases so it isn't getting much of my attention). See the post GlassFish Embedded Reloaded, an appserver in your pocket for sample code (that you could use from your tests) or Using maven plugin for v3 embedded glassfish (if you are using maven).
Another option would be to package and deploy your application with Cargo and then run some tests against the deployed application (with Selenium or a BDD tool for example). This could be useful if you want to run end-to-end tests with a container that doesn't provide any embedded API.
So, to answer your last question, I would indeed use available tools, maybe a combination of them, for tests that are not unit tests and wouldn't mock/inject stuff myself, except if they don't cover some needs that I can't think of right now.

http://stackoverflow.com/questions/1947846/how-do-you-unit-test-java-ee-code

Friday, June 8, 2012

Testing Private Methods in Java

I’ve been writing a lot of Java lately, and a lot of tests. We always write tests, right? Alas, no cool record-and-playback stuff like FlexMonkey or FoneMonkey, just plain old JUnit 4 tests with plently of Hamcrest goodness.
Suddenly, I realized that I really needed to test some private methods. So, a quick google for “testing private methods java” brings up a good article by Bill Venners. He lists all possible options to test private methods:
  1. Don’t test private methods
  2. Give the methods package access
  3. Use a nested test class
  4. Use reflection
Basically, the only real one is #4, use reflection. Bill didn’t give me the exact code I needed, so lots of googling later I realized that the world is filled with opinionated people (like me), and boy do they love to talk about #1. I just wanted some code, not a lecture, so I had to write my own code. Here is that code for anyone else that just wants to test private methods.
Imagine you have a class MyClass and it has a private method, myMethod(). Sorta like this:
public class MyClass {
    private String myMethod(String s) {
        return s;
    }
}
Then you could use reflection to invoke the method like this:
MyClass myClass = new MyClass();
Method method = MyClass.class.getDeclaredMethod("myMethod", String.class);
method.setAccessible(true);
String output = (String) method.invoke(myClass, "some input");
The real magic is setAccessible(true) which allows the private method to be called outside the class. And shazam, I can now test all my private methods. I was really hoping JUnit 4 would provide some additional facilities specifically for testing private methods, but not luck.

http://saturnboy.com/2010/11/testing-private-methods-in-java/

How do you unit test private methods?

You generally don't unit test private methods directly. Since they are private, consider them an implementation detail. Nobody is ever going to call one of them and expect it to work a particular way.
You should instead test your public interface. If the methods that call your private methods are working as you expect, you then assume by extension that your private methods are working correctly.

http://programmers.stackexchange.com/questions/100959/how-do-you-unit-test-private-methods

Tuesday, May 29, 2012

Creating Mock Tests: Using Easy mock


Unit testing is now a "best practice" for software development. In this unit testing we have to face so many situations where we need to interact with Database or any other resources. But at the same time we need to make our Tests isolated too. Here comes the importance of Mock objects.
Mock objects are a useful way to write unit tests for objects that act as mediators. “Instead of calling the real domain objects, the tested object calls a mock domain object that merely asserts that the correct methods were called, with the expected parameters, in the correct order.”
Using EasyMock Framework
EasyMock is a framework for creating mock objects using the java.lang.reflect.Proxy object. When a mock object is created, a proxy object takes the place of the real object. The proxy object gets its definition from the interface or class you pass when creating the mock.
EasyMock is providing two APIs for creating mock objects that are based on interfaces, the other on classes (org.easymock.EasyMock and org.easymock. classextensions.EasyMockrespectively).
We can separate the EasyMock implementation into FOUR steps 
1. Creating a Mock Object using “EasyMock.createMock”.
Create Mock : Using this static method we are creating a mock object. And this is the first step which we need to do in mock testing.
When we creates this mock objects then we can follow three levels.
Regular: If we are expecting some methods to be executed then it was not executed then the test will fail.  And if any unexpected tests executed then also test will fail. Here the order of the method execution is not important.
Ex: EmpDAO empDAO = EasyMock.createMock(EmpDAO.class);
Nice: If we are expecting some methods to be executed then it was not executed then the test will fail. And if any unexpected tests executed then it will return a default value. Here also order is not important.
Ex: EmpDAO empDAO = EasyMock.createNiceMock(EmpDAO.class);
Strict: Same as regular but here the Order of the expected methods also important.
Ex: EmpDAO empDAO = EasyMock.createStrictMock(EmpDAO.class);
2. Expecting mock object method calls using “EasyMock.expect”.
This is used to expect some method calls from our mock object. Lets go through one example.
Lets assume we have the following methods which gets employee information from DB.
List employee = empDao.getEmpDetails();
List employee = empDao.getEmpDetailsByName(“bond”);
In the unit test we need to follow as follows…
EmpDao mockDao = EaskMock.createMock(EmpDao.class);
Employee mockEmp = new Employee();
mockEmp.setEmpName(“bond”);
mockEmp.setEmpCode(“007”);
List empList= new ArrayList(1);
empList.add(mockEmp);
expect(mockDao.getEmpDetails()).andReturn(empList);
expect(mockDao.getEmpDetailsByName(“bond”)).andReturn(empList);
replay(mockDao);
3. Registering/replaying expected methods using “EasyMock.replay”.
Once the behavior of the mock objects has been recorded with expectations, the mock objects must be prepared to replay those expectations. We are using replay() method for this purpose. EaskMock will stop the expecting behavior once this method calls.
EasyMock.replay(mockDao);
4. Verifying the expected methods using “EasyMock.verify”.
Veirfying the mock expectations is the final step which we need to follow. This includes validating that all methods that were expected to be called were called and that any calls that were not expected are also noted.
EasyMock.verify(mock);
Easymock is providing more functionalities like “Matchers” etc for more unit testing flexibility. EasyMock has been the first dynamic Mock Object generator, relieving users of hand-writing Mock Objects, or generating code for them. It helps us to increase our testing coverage a lot.

http://orangeslate.com/2009/01/02/creating-mock-tests-using-easy-mock/