Saturday, July 23, 2011

AmIT's take on Java: How to mock static and final methods while writing...

AmIT's take on Java: How to mock static and final methods while writing...: "Many people will argue that if you are mocking static or final method in your Junit that means something is not correct with the way source ..."

Sunday, July 10, 2011

How to add code Syntaxhighlighter to your blog or website

I wish we had access to wiki like syntax everywhere. Until now I never paid attention on syntax high-lighting but a quick search on the topic led me to the discovery of Alex Gorbatchev. Please visit the website here http://alexgorbatchev.com Apache, SpringSource and Worldpress etc are using this. This is cool stuff :)

System.out.println("Hello World");

To do the above you would simply use the java brush.
System.out.println("Hello World");

To make this work you just have to copy paste following code after the <title></title> tag in your html or if using blogspot.com then go to Design-->Edit html

<!-- SYNTAX HIGHLIGHTER --> 
<link href='http://opentoastproject.googlecode.com/svn/trunk/dp.SyntaxHighlighter/Styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://opentoastproject.googlecode.com/svn/trunk/dp.SyntaxHighlighter/Styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://opentoastproject.googlecode.com/svn/trunk/dp.SyntaxHighlighter/Scripts/shCore.js' type='text/javascript'/>
<script src='http://opentoastproject.googlecode.com/svn/trunk/dp.SyntaxHighlighter/Scripts/shBrushJava.js' type='text/javascript'/>
<script src='http://opentoastproject.googlecode.com/svn/trunk/dp.SyntaxHighlighter/Scripts/shBrushJScript.js' type='text/javascript'/>
<script language='javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = &#39;http://opentoastproject.googlecode.com/svn/trunk/dp.SyntaxHighlighter/Scripts/clipboard.swf&#39;;
SyntaxHighlighter.all();
</script>
<!-- END SYNTAX HIGHLIGHTER -->

More stuff...
You can also get more brushes specific to your code from Alex's website e.g. sql, xml etc.

One thing you will stumble on is the different versions of javascripts. The one that I like and Spring is also using is 2.1.364 however the current version right now is 3.083 You can play around to see which one you like.

The best way is to link these files directly from his website e.g.
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css" />

Happy Blogging!

Saturday, July 9, 2011

How to mock static and final methods while writing JUnit?

Many people will point out that if you are mocking static or final method in your Junit that means something is not correct with the way source code is written and I totally concur with that. However sometimes you may end up in a situation where you have to mock static or final methods e.g. most of the vendor specific framework methods are declared as final.

The solution comes with PowerMock which is an extension to Mockito. PowerMock is the elixir for most of your advanced mocking/Junit needs. It comes with a cost of additional setting overhead though and can be intimidating at first. Please see the example below.


Problem Scenario:
Here is how the source code look:

public class MyClassA
{
 public static long timeTaken(long starTime)
 {
   // want to mock this currentTime() method call.
   return currentTime()+days;
 }

 public static long currentTime()
 {
   return System.currentTimeMillis();
 }
.
Solution:
Lets say we are testing timeTaken() method and would like to mock currentTime() method then here is how you could do that using PowerMock

@RunWith(PowerMockRunner.class)
@PrepareForTest(MyClassA.class)
public class MyClassATest
{
 @Test
 public void timeTaken()
 {
   long whateveryouwant = 1L;
   PowerMockito.spy(MyClassA.class);
   PowerMockito.when(MyClassA.currentTime()).thenReturn(whateveryouwant);
   long xxx = MyClassA.timeTaken(..);
   assertEquals(...);
 }

Here is what you need to have in your Pom if using Maven. (Please look the latest revision in http://repo2.maven.org/ )


      org.powermock
      powermock-module-junit4
      1.4.6
      test
    
    
      org.powermock
      powermock-api-mockito
      1.4.6
      test