Skip to main content

Posts

Showing posts with the label best practice

Unit Tests - Why are they so important?

Why do we need Unit Testing? There are two important reason : To test different scenarios and making sure the logic we have just written works as expected In case a developer is changing the logic in future, they notice whether they have broken the existing logic or not What Should Be Tested? Different scenarios: E.g. each if-else statement can be taken as a separate scenario Data that we expect to be changed within a method Possible exceptions that may be thrown What Does Not Need to Be Tested? Mocked data that we created as input data Any result from a third party API. E.g: Hibernate, Spring etc. Results from classes that are already tested in their own unit tests. Example public Employer save(Employer employer) { if (employer.getActivateFlag() != null ) { if (employer.getActivateFlag()) { employer.setStatus(Status.Active); } else { employer.setStatus(Status.Inactive); } ...

Hibernate And Mapping enum to customized values

With Hibernate, enums can be easily mapped either by enum item name or the position of each item but what if you want to map it to a customized value? In my case, we have so many one-character long columns in our tables, representing flags, statuses etc. We have heaps of them. Writing UserTypes for each enum field is very boring and nasty job. As every where you see in internet, you need to keep a Map for each user-type in order to map those values to enum elements. So to avoid this, I ended up with something more clean, easy and more generic. Now imagine you have following enum: public enum PaymentFrequencyEnum { WEEKLY("WK"), FORTNIGHTLY("FN"), MONTHLY("MT"), QUARTERLY("QL"), YEARLY("YL"); private String value;     private PaymentFrequency(String value) { this.value = value; } } I've chosen two-letter code as value so that you understand m...

Introduction

Hi My name is Mohammad. I am a passionate Java developer. When I was in university, I was very interested in Borland Delphi and Turbo C++ they were hot at those ages but I ended up with my first job as Oracle Developer. I was lucky because Delphi was about to die and now we can call it an ancient programming language! It is back to its hometown: Delphi Temple :) So after two years working with Oracle Form Builder and Oracle database, I attracted to Java. So first thing I learned was Apache Struts 1.1. It was awesome however now every body smiles when you mention it :) Very soon I switched to JSF and JBoss. But they were very young at that time. I also worked with Beehive from Apache which nobody may heard about it :) I worked with Apache Lucene, JSF and JBoss Seam for about two years and then I packed my bag to work in back end. I love back end programming! My best years of working was in a Banking software company which I had oppurtunity to design and develop some big applic...