Thursday 14 June 2007

JMockit : Non-public mock class

Running a simple unit test, with the 'new Object()' syntax.


public void test()
{
Mockit.redefineMethods(emeraldjava.impl.ApplicationCase.class, new Object() {
public void method()
{
System.out.println("JMocked");
}
});
}
ApplicationCaseTest appCase = new ApplicationCaseTest(getName());
assert(appCase.method(),..);
}


I got this error.


java.lang.IllegalArgumentException: Non-public mock class
at mockit.Mockit.collectMockMethods(Mockit.java:182)
at mockit.Mockit.redefineMethods(Mockit.java:170)
at mockit.Mockit.redefineMethods(Mockit.java:124)
at emerabldjava.workflow.ClaimIntakeTest.test2(ClaimIntakeTest.java:92)
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:585)
at junit.framework.TestCase.runTest(TestCase.java:166)
at junit.framework.TestCase.runBare(TestCase.java:140)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:131)
at junit.framework.TestSuite.runTest(TestSuite.java:173)
at junit.framework.TestSuite.run(TestSuite.java:168)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


Solution

Use an named class


public Test extends TestCase
{

public class AppMock
{
public void method()
{
System.out.println("JMocked");
}
}

public void test()
throws AppException, InformationalException, ClassNotFoundException
{
Mockit.redefineMethods(emeraldjava.impl.ApplicationCase.class, new AppMock());
UICApplicationCaseTest appCase = new UICApplicationCaseTest(getName());
assert(appCase.method(),..);
}
}

Sunday 10 June 2007

Blogger with Google Analytics & Sitemap

Just a couple of quick links to two tutorials on setting your blog (or any website) up with statistics and indexing.

Blogger And Analytics outlines how you can see the hits your site recieves, using Google Analytics.

To ensure that the context of your site is index for the google search engine you should follow the Blogger and Sitemap tutorial, which uses the Google SiteMap.

Saturday 9 June 2007

Blogger & Google Code Prettify

I wanted to improve the look of my java source code examples that i have in my blog posts. I came across the google-code-prettify project. The main hassel is the java script and css files need to be included in the blog template. A bit of investigation and i noticed i could link the two required files from the project web site. [Granted, if they change the location of the files we're screwed.]

1: Add the java script file to the blog template.

<script src="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js" type="text/javascript">


The complete java script src file path : http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js

2: Link the css file to the blog template.

<link rel="stylesheet" type="text/css" ref="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css"/>


The complete css src file path : http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css

3: Update the blog template to call the prettyPrint() function when loaded.

In the blog template file add the call to the prettyPrint() function to the body onload tag.

<body onload="prettyPrint();">


4: Use the prettyprint command in your blog post.

<pre class="prettyprint" id="xml">
your code
<pre>


Links

Duffblog 2.0: Google Code Prettify

Wednesday 6 June 2007

JMockit

The business class we want to test


package emeraldjava.jmockit;

/**
* Service.
*/
public class Service {

public boolean doBusinessWork()
throws Exception
{
Workflow workflow = new Workflow();
if(workflow.startProcess()==1)
return true;
else
return false;
}

}


The object used by the service class


package emeraldjava.jmockit;

public class Workflow {

public long startProcess()
throws Exception
{
return 1;
}

}


The test class


package curam.uic.tools.jmockit;

import junit.framework.TestCase;
import mockit.Mockit;

public class ServiceTest extends TestCase {

public ServiceTest(String name)
{
super(name);

}

public void test()
throws Exception
{
Service service = new Service();
assertEquals("",true,service.doBusinessWork());
}

public void test2()
throws Exception
{
Service service = new Service();

Mockit.redefineMethods(Workflow.class, new Object() {
public long startProcess()
{
return 2;
}
});
assertEquals("",false,service.doBusinessWork());
}

public static class WorkflowMock
{
public long startProcess()
throws Exception
{
throw new Exception();
}
}

public void test3()
{
Service service = new Service();
Mockit.redefineMethods(Workflow.class, WorkflowMock.class);

try
{
service.doBusinessWork();
}
catch(Exception e)
{
assertNotNull("Exception thrown",e);
}
}
}