Wednesday, November 26, 2014

Why do all but first test fail - EasyMock

The answer is probably very obvious most of the time. The first test did one of the following
- changed the state of objects that were used by other tests
- the first test consumed expectations on shared mocked objects
- mock controls were set but not reset at the end of the first test

I ran into this one today. I had all tests passing when running them independently but only the first one passing when running all tests in the file. Having made sure I was resetting the mock controls and the expectations were set correctly, I knew it had to be the first one where the status of objects were changed.

The problem was that my object under test was a singleton class. The first test created the object under test based on mocked arguments in its constructor and the test passed.

However, when the second test was run, despite the fact that I had reset the IMocksControl object in my test, the expectation in my second test were not being  met. You can probably already see what is happening here. The object under test is the same instance (as in the first test - given that its a singleton) and the mocked objects that I passed in during the first test persisted. So when the second test 'expected' things to happen, the expectations were never fulfilled because they were already consumed by the first test. Regardless of whether the object under test was a member variable or a local variable for each test, it did not make any difference.

I fixed the issue by cautiously creating setters with default access so that the second test could supply new set of mocked objects to the object under test. This takes me back to the sameissue that I have never felt comfortable with - exposing for test purpose - but have done it anyway in multiple instances.

Wednesday, November 19, 2014

Testing Thread static method using Powermock

In one of my previous posts, I had talked about mocking static methods using Powermock. Today, I got thrown off a bit when the same technique worked without any issues for any other static methods that I have used so far but when it came to verifying that a 'task' paused for a certain amount of time, I wasn't able to test it and it simply executed the Thread.sleep(xxx) as if no mocking attempt had been made. Although, I wouldn't say I 'solved' the problem, I did find a way to deal with this.

The solution is in fact VERY simply. I extracted the Thread.sleep call to an utility class. I then went ahead an mocked my Utility class and 'expected' the call to sleep method.

public static void sleep(long timeToSleep) throws InterruptedException {
    Thread.sleep(timeToSleep);
}


Instead of catching InterruptedException in the Utility method, the exception is caught in the method that calls this method. This way I can test situation where I throw the exception for the test.

PowerMock.mockStatic(ThreadUtils.class);
ThreadUtils.sleep(600000l);
expectLastCall().once().andThrow(new InterruptedException());

Tuesday, November 11, 2014

Use velocity to generate XML requests

If you are interacting with third party services, there is a chance you will need to write up your request is XML format with details. I will show you how to use Velocity to generate your requests using an XML template.

Get the java library for Velocity Engine and add it to your class path.

Create a .vm file with a template


<MyRequest>
    <name>$name</name>
    <value>$double.doubleValue()</value>
    <action>$action</action>
</MyRequest>



Java Code

VelocityEngine engine = new VelocityEngine();
engine.init();

VelocityContext context = new VelocityContext();
context.put("name", "GET");
context.put("action", "someAction");
context.put("double", new Double(4));

Template template = engine.getTemplate(requestTemplateFile);

StringWriter writer = new StringWriter();
template.merge(context, writer);

System.out.println(writer.toString());

Your resulting xml will be like below


<MyRequest>
    <name>GET</name>
    <value>4.0</value>
    <action>someAction</action>
</MyRequest>

  

Wednesday, November 5, 2014

How do delete remembered selected item from browser

When you use a website regularly which requires searches, you will soon end up with whole lot of words that accumulates and come up as suggestions. This might be a good thing at times and you don't have to type the whole work again.

However, imagine typing a wrong email address on your favorite website, pressing the Return (Enter) key and finding out that it always show up next time you want to use the website. Or, at times you just don't want to see the suggestion without having the delete all the browser caches.

All you need to do is
1. Bring up the suggestions by either starting to type the work or by double clicking on the input field. Do not click it
2. Scroll using the keyboard (down arrow) or using the mouse by simply hovering over it.
3. Press Shift+Delete key.


Decompiler in IntellJ

JetBrains  released IntelliJ 14 just yesterday. As mentioned in the release notes, IntelliJ now has built in decompiler. It makes it really easy to view the source code for external libraries you are using. Although the variable names read var1, var2...varN, it is still an excellent experience.