Showing posts with label jUnit. Show all posts
Showing posts with label jUnit. Show all posts

Tuesday, September 13, 2011

Testing Private methods (with Collection parameter) - JUnit

Class having a private method which needs to be tested:


package com.practice;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

public class ClassWithPrivateMethod {
    public static void main(String[] args) {
        // ....
    }
   
    private void findOwnerForThisAddress(List<People> peopleList, Address address) {
        // Some logic find out who out of peopleList stays in this address.
    }

}

class People {
   
}

class Address {
   
}

JUnit test class to test the private method: 

class PrivateMethodTest {

    ClassWithPrivateMethod classOfInterest;
   
    @Before
    public void setUp() throws Exception {
        classOfInterest = new ClassWithPrivateMethod();
    }
   
    @Test
    public void testFindOwnerForThisAddress() {
        Address address = new Address();
        List<People> peopleList = new ArrayList<People>();
       
        try {
            Method methodOfInterest;
            methodOfInterest = ClassWithPrivateMethod.class.getDeclaredMethod("findOwnerForThisAddress", List.class, Address.class);
            methodOfInterest.setAccessible(true);
           
            methodOfInterest.invoke(classOfInterest, peopleList, address);
           
        } catch (SecurityException e) {
            Assert.fail(e.getMessage());
        } catch (NoSuchMethodException e) {
            Assert.fail(e.getMessage());
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        } catch (IllegalAccessException e) {
            Assert.fail(e.getMessage());
        } catch (InvocationTargetException e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

}



Monday, June 6, 2011

Continuous unit test runner

Came across the interesting Eclipse plug-in Infinitest, which on change of a source java file, runs the unit tests corresponding to that class.
If your class has good unit test coverage, you'll get to know the potential effect of your change on other dependant functionalities as soon as you save your changes on local.


You don't need to wait till you 
1) Build your project to see the breaking unit tests and
2) Do functional test to identify if your changes impact any other functionality (provided your class has good unit tests).


To add this plug-in to eclipse, just add the update site: http://infinitest.github.com


For more details, example video, you can verify this blog --> http://www.vasanth.in/2011/06/03/infinitest-a-continuous-test-runner-for-java/


Additional links:
http://infinitest.github.com/index.html
http://improvingworks.com/


Copy paste of text from Vasanth's blog:
Infinitest is an Eclipse plugin that runs the JUnit tests instantly when you save a Java file. Check out the demo above. It smartly runs only the test affected by the file you just saved. This is cool. You can immediately know if you broke something as soon as you save your code. Of course, your test cases should be comprehensive. Improving Works developed this plugin and has released it as a open source project.

Monday, March 14, 2011

JUnit throws java.lang.IllegalArgumentException: Not a mock

EasyMock comes with two flavors of replay and verify.
If you have mocked a concrete class and used below imports you are bound to get the exception mentioned in the end of post.
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;


Above flavor supports Interfaces only.

The other flavor of replay and verify is:
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;


This supports both Concrete class and Interface!

Hence always better to use below imports while mocking using EasyMock:
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;




java.lang.IllegalArgumentException: Not a mock: com.ragsetty.practice.SampleTest$$EnhancerByCGLIB$$3fc916d6
at org.easymock.internal.ClassExtensionHelper.getControl(ClassExtensionHelper.java:66)
at org.easymock.EasyMock.getControl(EasyMock.java:2068)
at org.easymock.EasyMock.replay(EasyMock.java:1970)
at xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Monday, February 7, 2011

JUnit 4 TestSuite Declaration

package com.ragsetty.practice;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
MyFirstTest.class,
MySecondTest.class
})
public class SomeClassName {
// This is just to compile and run. Nothing is required in this class.
}

JUnit Articles

Evil Unit Testing: http://www.javaranch.com/unit-testing.jsp