Tuesday, 29 November 2011

Easymock - create partial mocks

EasyMock is a very useful tool allowing to mock all objects that the tested unit of code depends on. This is of course assuming you write your code in the way that allows that e.g. using dependency injection pattern.

It is also possible to mock only a part of the object e.g. single method and leave original implementation for the remaining part. Such object is called a 'partial mock'.

The following Java code presents how to create such partial mock:
package com.blogspot.fczaja.samples

import org.easymock.EasyMock;
import org.junit.Test;

public class PartialMockTests
{
class PartialMock
{
void foo()
{
// Code inside foo() will not be invoked while testing
System.out.println("foo");
}

void boo()
{
// Code inside boo should be invoked
System.out.println("boo");
foo();
}
}

@Test
public void testPartialMock()
{
PartialMock partialMock = EasyMock
.createMockBuilder(PartialMock.class) //create builder first
.addMockedMethod("foo") // tell EasyMock to mock foo() method
.createMock(); // create the partial mock object

// tell EasyMock to expect call to mocked foo()
partialMock.foo();
EasyMock.expectLastCall().once();
EasyMock.replay(partialMock);

partialMock.boo(); // call boo() (not mocked)

EasyMock.verify(partialMock);
}
}
When executing our test the method foo() of the object will be mocked and method boo() will be invoked normally. The console output of that test would be:
boo

I'm using this technique when I want to test a single method, that calls other methods in the same class. I can mock all other methods so they behave like methods form other mocked objects.

Friday, 4 November 2011

The data set DataSet1 is a shared dataset. SQL Server 2008 Reporting Services does not support shared data sets

I was working on a SSRS project using Sql Server 2008 but decided to upgrade to R2 version, so I can share commonly used datasets across all reports. After the upgrade I converted my SSRS project to the new version and converted existing datasets to shared. When I try to build the project I got the following error:

The data set, DataSet1, is a shared dataset. SQL Server 2008 Reporting Services does not support shared data sets

Hmm, did the upgrade or conversion fail?

Solution
It came out you need to update one of the project settings called: TargetServerVersion. To do that right click your SSRS project, select Properties and search for that setting:


The correct value to be set is "Sql Server 2008 R2".