Jump to content

User:Ssharm17/Needle

From Wikipedia, the free encyclopedia
Needle
Developed byAkqinet
Current VersionVersion 2.3
First VersionVersion 2.1
Official Websitehttp://needle.spree.de/
GitHubhttps://github.com/tomas/needle

Needle

[edit]

Needle is a lightweight framework mainly used to perform unit testing in Enterprise Java applications[1]. It was developed by a company, Akqinet. Currently needle4j is the latest version of Needle namely version 2.3. The source code for Needle, and needle4j is available on GitHub. Markus Dahm, Carsten Erker, Heinz Wilming, and Jan Galinski are the developers with commit privileges for all the versions of Needle. Needle performs unit testing of Java program components by injection of "Mock Objects". It can be used with JUnit or TestNG and supports EasyMock and Mockito mocking frameworks.

History

[edit]

The first version of Needle was Needle version 2.1. This version gave syntax errors while parsing the query and did not implement injection of Mock Objects in test cases. Needle version 2.2 was implemented with this added feature of adding Mock Objects. However, this version created multiple Mock Objects for an Injection Target of the same type. needle4j that is Needle version 2.3 fixed this bug and also updated the support for JUnit 4.11.[2]

Features[3]

[edit]
  • Instantiation of @ObjectUnderTest components.
  • Constructor, method and field based injection.
  • Injection of Mock objects by default.
  • Extensible via custom injection providers.
  • Wiring of object graphs.
  • Database testing via JPA provider, e.g. Hibernate.
  • EntityManager creation and injection.
  • Execute database operations in setup and teardown.
  • Transaction utilities.
  • Utilities for reflection.
  • Can be used with JUnit or TestNG.
  • Supports EasyMock and Mockito out-of-the-box.

Installation

[edit]

needle4j is the current version of Needle which can be integrated with Java Enterprise Development applications.[4]. A requirement for using this library is having JDK6+ installed. The first way to integrate needle4j is to download the source files as a zip file from GitHub. The .jar file can be then added to the project by configuring the build path of the project. This is not the recommended method as it does not support project migration.The second way is to add dependency information for needle4j to the project. Following are some the ways of adding dependencies in needle4j, depending on the type of project. needle4j can be used with Apache Maven, Apache Buildr, Apache Ant, Groovy Grape, Grails, Leiningen, and SBT.[5] In addition to the default dependencies provided by needle4j, some more dependencies must be added to compile and run the application as well as to run unit tests.[6]

For example, to use needle4j with projects built using Apache Buildr and Grails the following syntax should be used.

Apache Buildr
[edit]
'org.needle4j:needle4j:jar:2.3'
Grails
[edit]
compile 'org.needle4j:needle4j:2.3'


needle4j's default configuration maybe changed using the needle.properties file in the root classpath. The configuration of Mock Object provider, database operation, JDBC connection can be changed by a user. For creating logs, needle4j uses the Simple Logging Facade for Java (SLF4J). The dependency information of SLF4J can be added to use it with needle4j.[7]

Testing

[edit]

Needle provides testing for all the components of a Java project including a separate testing for database connectivity.[8] While testing components, Needle may instantiate objects itself (Mock Objects) or the programmer can instantiate the objects (Real Objects). Needle performs testing by dynamically creating Mock Objects for the dependencies of the components that have to be tested. It has built-in implementations for EasyMock and Mockito, to use other frameworks “de.akquinet.jbosscc.needle.mock.MockProvider” interface must be implemented and accordingly configured in the Needle properties file.

To create Mock Objects in Needle a field with the annotation @Mock is used. The corresponding injection provider automatically provides the dependencies for the object under test.

Database Testing

[edit]

While performing Unit Tests in an application it is recommended to perform test to check database connectivity by using test cases. Even though tests are performed on the test databases they still need to be performed on real database to ensure that the tests work as expected.

In such cases Needle helps by automatically creating and injecting the Entity Manager instance into the objects that are to be tested.[9] For this the user only has to provided persistence.xml and the JDBC driver to the class path of test execution. The persistence.xml file is a standard configuration file in Java Persistence API (JPA). While accessing the database using the Entity Manager for a transaction many operations are to be started, which may not have been started on the test component as they are usually maintained by the application server. To run the code using transactions the TransactionHelper Utility makes it convenient. While performing a unit test on databases , the test cases expect some state of data before the execution of the test and the database is usually altered after the execution. To address this problem Needle provides an option of doing additional cleaning of data after performing tests.

Compatible Software

[edit]
JUnit
[edit]

When testing is done by using a combination of Needle and JUnit, mainly the testing is done by JUnit, but Needle provides all the Mock Objects required for Testing.

TestNG
[edit]

Needle supports TestNG by extending its classes to concrete test classes. There are two classes which are extended namely - de.akquinet.jbosscc.needle.testng.AbstractNeedleTestcase and de.akquinet.jbosscc.needle.testng.DatabaseTestcase.

[edit]

GitHub needle4j

GitHub Needle

Akquinet

References

[edit]
  1. ^ "Needle Documentation".
  2. ^ "Needle Change Log".
  3. ^ "Needle Overview".
  4. ^ "Needle4j website".
  5. ^ "needle4j dependency information".
  6. ^ "needle4j project dependencies".
  7. ^ "needle4j configuration".
  8. ^ "Needle Homepage".
  9. ^ "needle4j Database Testing".