Monthly Archives: February 2014

When the Spring Security Principal isn’t enough in a Spring MVC Controller

I had previously made a blog entry to retrieve the spring security principal via @AuthenticationPrincipal in a controller method. Getting the Spring Security Principal in a Spring MVC Controller method. That is useful if you need the username or the … Continue reading

Posted in Java, Spring, Spring MVC, Spring Security | 3 Comments

Mocking Spring Security Principal for testing Spring MVC Controllers

How do you mock a Spring security principal for testing spring MVC controllers? Create a spring security principal Create a spring authentication Add the spring security authentication to the security context The last step should not be needed because MockMVC … Continue reading

Posted in Java, MockMVC, Spring, Spring MVC, Spring Security | Tagged , , , , , , , , , , , , , , | 2 Comments

Testing Spring MVC Rest controllers

Spring MVC 3.2 introduced the ability to test controllers via MockMVC. In this example, we will test a controller that returns JSON via @ResponseBody. Here are the dependencies that you will need. This is an example of the spring context. … Continue reading

Posted in Java, MockMVC, Spring, Spring MVC | Tagged , , , , , , , | Leave a comment

Velocity templating notes

The documentation is here An if statement looks like this An if-elseif-else looks like this A for loop looks like Values can be output by ${…} or $… There is a particular case where the ${…} notation should be … Continue reading

Posted in templating, velocity | Tagged | Leave a comment

How to get the absolute file path of a file loaded with a class loader

Posted in Java | Tagged , , , , , , , , , | Leave a comment

Joda getting a date without a time (midnight)

LocalDate localDate = new LocalDate(); localDate.toDateTimeAtStartOfDay();

Posted in Java, Joda-Time | Leave a comment

Get the active Spring profiles

Inject the Environment object The Environment object has getActiveProfiles that returns an array of active profiles. To get the first active profile, do…

Posted in Spring, Spring Profile | Tagged , , , | Leave a comment

How to see what Maven dependencies have newer versions

The output is useful, but you still need to interpret it. In this case, there is a beta library that is suggested. Not. In this case, a library with a version number that doesn’t follow the normal version patters is … Continue reading

Posted in Maven | Tagged , , , , | Leave a comment

Creating a list of strings in your Spring context

A friend of mine had to use this and I thought it was something worth remembering. He injected them with SpEl

Posted in Java, Spring | Tagged , , | Leave a comment

Expected exceptions in Junit

I had seen annotating a test class with @Test(expected = Exception.class) to test for exceptions before. In this case, we needed to test for Exception and the message was made the difference. This can be accomplished by @Rule

Posted in Integration Tests, Java, JUnit, Test Driven Development | Tagged , , , , , , , , , , | Leave a comment