Showing posts with label JUnit. Show all posts
Showing posts with label JUnit. Show all posts

Monday, 28 October 2013

ExpectedException JUnit 4.9

The ExpectedException Rule allows in-test specification of expected exception types and messages: Example here: http://junit.org/javadoc/4.9/org/junit/rules/ExpectedException.html

Monday, 19 August 2013

FEST library for fluent assertions in junit tests

Here you can read more about library that make junit tests more readable using fluent assertions: https://code.google.com/p/fest/

Sunday, 18 August 2013

Monday, 4 February 2013

Testing Spring services that uses scoped objects

I have faced problem of testing Java Services that used Session Scoped objects (Same would by with f.e. Request Scope). My Scoped object was treated as a data, but contained simple operations managing its state. My service did some operation on this object changing it state. First I tried to mock this object and mocking its methods to verify correct behavior. But it was not easy due to complicated structure of mocking object. Then I decided to use a Spy object, but the only reason is to inject this object into my Service with annotation @InjectMocks. I did not change my object at all in the Spy, but used it just for dependency injection, as I did not want to have a setter method only for testing.
So the conclusion is: In Mockito Session/Request Scope Objects can be easily tested when defined as Spy objects injected by @InjectMocks

Sunday, 4 November 2012

Defining Mockito mock bean from Spring xml definition

You can define a spring bean in spring xml context that will be a Mockito mock of a given type:

  

But if you want to inject that bean by type, you have to define it as a Proxy of given interface:

    
      
        
      
    
    
      com.example.Repository
    
  

Saturday, 25 August 2012

DirtiesContext

Test annotation which indicates that the ApplicationContext associated with a test is dirty and should be closed:
  • after the current test, when declared at the method level
  • after each test method in the current test class, when declared at the class level with class mode set to AFTER_EACH_TEST_METHOD
  • after the current test class, when declared at the class level with class mode set to AFTER_CLASS
Read more here: http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/test/annotation/DirtiesContext.html

Tuesday, 31 July 2012

ArgumentCaptor in Mockito

Use it to capture argument values for further assertions.
It should be used during verifications. It is very useful when assert on argument values to complete verification.
It can also be fined through annotation: @Captor
More info here: http://docs.mockito.googlecode.com/hg/org/mockito/ArgumentCaptor.html
and : http://docs.mockito.googlecode.com/hg/org/mockito/Captor.html

Mockito Matchers

When using Mockito you can use MockitoMatchers. You can use predefined ones or create own implementation.
F.e.
assertThat(object, CoreMatchers.equalTo(0));
More examples as well as own implementations here: http://docs.mockito.googlecode.com/hg/org/mockito/Matchers.html

Sunday, 15 July 2012

Wiser - for testing mails by JUnit

Wiser is a simple SMTP server that you can use for unit testing applications that send mail. It accepts all messages and stores them in an internal ArrayList. Your unit test code can then examine the messages (or lack thereof) and verify their validity.
More on: http://code.google.com/p/subethasmtp/wiki/Wiser

Friday, 13 July 2012

XmlUnit - JUnit testing for XML

XMLUnit for Java provides two JUnit extension classes, XMLAssert and XMLTestCase, and a set of supporting classes (e.g. Diff, DetailedDiff,Transform,SimpleXpathEngine,Validator,NodeTest) that allow assertions to be made about:
  • The differences between two pieces of XML
  • The outcome of transforming a piece of XML using XSLT
  • The evaluation of an XPath expression on a piece of XML
  • The validity of a piece of XML
  • Individual nodes in a piece of XML that are exposed by DOM Traversal
XMLUnit for Java can also treat HTML content (even badly-formed HTML) as valid XML to allow these assertions to be made about the content of web pages too.

More information in the tutorial: http://xmlunit.sourceforge.net/userguide/html/index.html

JUnitParams - librabry for JUnit

This project adds a new runner to JUnit and provides much easier and readable parameterised tests for JUnit >=4.6.

Main differences to standard JUnit Parameterized runner:

  • more explicit - params are in test method params, not class fields
  • less code - you don't need a constructor to set up parameters
  • you can mix parameterised with non-parameterised methods in one class
  • params can be passed as a CSV string or from a parameters provider class
  • parameters provider class can have as many parameters providing methods as you want, so that you can group different cases
  • you can have a test method that provides parameters (no external classes or statics anymore)
  • you can see actual parameter values in your IDE (in JUnit's Prameterized it's only consecutive numbers of parameters):

More on project page: http://code.google.com/p/junitparams/