Performance Zone is brought to you in partnership with:

Enterprise Architect in HCL Technologies a $7Billion IT services organization. My role is to work as a Technology Partner for large enterprise customers providing them low cost opensource solutions around Java, Spring and vFabric stack. I am also working on various projects involving, Cloud base solution, Mobile application and Business Analytics around Spring and vFabric space. Over 23 yrs, I have build repository of technologies and tools I liked and used extensively in my day to day work. In this blog, I am putting all these best practices and tools so that it will help the people who visit my website. Krishna is a DZone MVB and is not an employee of DZone and has posted 64 posts at DZone. You can read more from them at their website. View Full User Profile

Designing a simple usecase using Test First Approach using Spring

02.17.2013
| 1632 views |
  • submit to reddit

 

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.


Published at DZone with permission of Krishna Prasad, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)