Tuesday, August 14, 2018
Spring PropertyPlaceholderConfigurer to read from AWS Parameter Store
Tuesday, August 22, 2017
Why is Spring @Value not working to read my parameter and default value?
Yet it was not working and the value was coming out as "$myparam:defaultVal".@Value("${myParam:defaultVal")private String myParam;
What's the issue?
Eventually i stumbled upon the issue via this spring issue. I also saw it addressed here although there is a mistake as it is not sufficient to use util:properties as explained here. So, to make this work if you are using XML just add this in:
<context:property-placeholder location="classpath:/myfile.properties"/>
Now hopefully this wont happen again to me...
Tuesday, June 13, 2017
Using Gradle for a mixed groovy and java project and creating a distribution zip
Ok but let's say i am writing both java and groovy in the project
In order to have this work, you need to configure your Gradle project with the following small changes so that you can make it compile the mixed code.
You need to make all the code compile as Groovy code (even the Java code).
So, add the following lines to your project:
sourceSets.main.java.srcDirs = [] sourceSets.main.groovy.srcDirs += ["src/main/java"]
This makes all your java code compile with the Groovy compiler to prevent dependency issues between the Groovy and Java code.
task zip(dependsOn: jar, type: Zip) { from { configurations.runtime.allArtifacts.files } { into("${project.name}-${project.version}") } from { configurations.runtime } { into("${project.name}-${project.version}/lib") } }
Now when you run gradle zip you will get a zip with all dependencies inside!
Tuesday, January 27, 2015
Why am I getting "WARNING arguments left: 1"
logger.warn("Exception caught: ", e);
logger.warning("Exception caught: {}", e);
Spring's StringUtils -- Stuff we do with Strings all the time
Check them out: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/StringUtils.html
There are many nice simple things that we need to do all the time:
- capitalize (first characters)
- uncapitalize
- Lots of utilities for String arrays -- adding to them, sorting etc
Tuesday, December 10, 2013
Taking mocking seriously
However, as I started using them and learning their power, I have seen that I can really use mocking in ways that enhance my unit tests. Here is a concrete example. I am using Spring Data JPA. So, I need to mock the save of my entity when it gets created in the function that I am unit testing. However, once I am mocking I can easily also validate the parameters sent to this function that did the save. Using Mockito, we use the ArgumentCaptor, to do this validation as follows:
This now means my unit test has validated that all parameters passed to the function were passed to the database.MyRepository myRepository = mock(MyRepository.class); ArgumentCaptor<EntityClass> argument = ArgumentCaptor.forClass(EntityClass.class); //... invoke function to unit Test that will internally save Entity verify(myRepository).saveAndFlush(argument.capture()); EntityClass entity = argument.getValue(); assert entity.getName().equals(“name”); assert entity.getAddress().equals("address");
Now we see some real added value in the mock, where we can be confident that our copy of the data to the entity we are persisting has not missed any of the parameters.
Another nice feature in mocking is checking how many times a function is invoked. Modifying the the code above:
MyRepository myRepository = mock(MyRepository.class); ArgumentCaptor<EntityClass> argument = ArgumentCaptor.forClass(EntityClass.class); //... invoke function to unit Test that will internally save Entity verify(myRepository,times(1)).saveAndFlush(argument.capture()); EntityClass entity = argument.getValue(); assert entity.getName().equals(“name”); assert entity.getAddress().equals("address");
Happy Mocking!
Thursday, May 26, 2011
Using Date and Calendar – At your own risk!
You can find lots of issues discussed on the ‘net about the problems of the Calendar/Date classes in Java. Here are a few that recently reared their ugly head in our code:
1. This one is actually not in either of these but in trying to output our Date. Beware of the mm instead of MM when using SimpleDateFormat. Yep, one of my team members used the following code:
SimpleDateFormat sdf = new SimpleDateFormat(“yyyymmdd”);
Surprisingly everyone in our tests had a birthday of the first of the month! I think that the right solution would be that mm is wrong and mi is minutes and mo is month. This would solve the confusion.
2. Did you know that there is a difference whether you use Calendar.HOUR or Calendar.HOUR_OF_DAY? Ok, i admit it this one is documented in the javdocs, but i would still think that it is a bit of surprise that in the afternoon, if you do
cal.set(Calendar.HOUR,0);
that you will get 12:00 PM!
3. Why does toString() of Calendar not put out the date in some format?
4. Beware that DAY_OF_WEEK is not zero based while MONTH is.