Designing a simple usecase using Test First Approach using Spring

In this blog I will demonstrate designing a usecase with Test First Development approach. Let me explain the usecase first, we need to write an application, where we need to recommend a user some products based on his past purchase history, in which he has similar brands. Get the working code from Github.

If you see in the above usecase, there are 3 objects, User, Item object, and UserItemPurchaseHistory.

Test First Development

As in the test first approach, let us write the test as below,

@Test
public void testUserRelevance() {

ArrayList<Item> productFromApiList = new ArrayList<Item>();

Item Item = new Item();
Item.setName("Ferrari 750 Monza");
Item.setBrand("Ferrari");
productFromApiList.add(Item);

Item = new Item();
Item.setName("Porsche 911 GT3");
Item.setBrand("Porsche");
productFromApiList.add(Item);

Item = new Item();
Item.setName("Cheverlet Corvette");
Item.setBrand("Cheverlet");
productFromApiList.add(Item);

Item = new Item();
Item.setName("Honda City");
Item.setBrand("Honda");
productFromApiList.add(Item);

Item = new Item();
Item.setName("Hynduai Verna");
Item.setBrand("Hynduai");
productFromApiList.add(Item);

HashMap<String, Object> map = new HashMap<String, Object>();

List<Item> productFromApiListOut = productResponseServiceImpl.filterAsPerUserRelevance(productFromApiList, 2);

assertEquals(productFromApiListOut.size(), 1);
assertEquals(productFromApiListOut.get(0).getName(),"Ferrari 750 Monza");
}

Now we need to do database configuration for testing purpose. We will use Spring to configure embedded inmemory database as follows,

<bean id="sqlMapClient">
<property name="configLocation" value="ibatis-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="ibatisTemplate">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>

<beans profile="dev">
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="classpath:SourceDB.sql" />
</jdbc:embedded-database>
</beans>

If you notice, there is SourceDB.sql where you have schema design and the seed data. Once you created the config file, you need to wire the same with the JUnit test as below and set the profile,


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml",
"classpath:test-datasource-config.xml"})
public class FilterAsPerUserFriendsRelevanceTest {

static {
System.setProperty("spring.profiles.active", "dev");
}

The implementation details of  filterAsPerUserRelevance method as below,

public List<Item> filterAsPerUserRelevance(List<Item> productListFilteredAsperUserTaste, int id) {
List<Item> recommendedProducts = getListFromRecommendationEngine(id);

ArrayList<Item> list3 = new ArrayList<Item>();

for (Item productFromApi : productListFilteredAsperUserTaste) {
for (Item item : recommendedProducts) {
if (productFromApi.getBrand().toLowerCase().equals(item.getBrand().toLowerCase())) {
list3.add(productFromApi);
}
}
}
return list3;
}

private List<Item> getListFromRecommendationEngine(int id) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("userId", id);

List<UserItemPurchaseHistory> list2 = ibatisTemplate.queryForList("GoSmarter.userItemList", map);

assertEquals(1, list2.size());
assertEquals(list2.get(0).getItemId(), 1);

map = new HashMap<String, Object>();
map.put("itemId", list2.get(0).getItemId());
Item item = (Item) ibatisTemplate.queryForObject("GoSmarter.itemDetails", map);
assertEquals(item.getName(), "FERRARI ENZO");

List<Item> list3 = new ArrayList<Item>();
list3.add(item);
return list3;
}

If you notice we get a input list of the items the user has to choose from. It retrieves the user item purchase history and compared the item brands and return the brands which the user has already purchased and asserts in the JUnit test.

In the test first development, we are testing first and than design the POJO and the associated DB schema and write the business logic and assert the output as per the Seed data.

Once the design is complete, we use a different profile to point to the actual database.

About these ads

One thought on “Designing a simple usecase using Test First Approach using Spring

  1. Pingback: Java Web Development Frameworks: Play 2.0 and Spring MVC | Spring under the hood

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s