Category Archives: Spring

Spring P-notation

Spring P-notation was made to make creating spring config files a little neater and shorter. Here is what a typical file would look like. Here is what it looks like once the P-notation is used.

Posted in Spring | Tagged , , , , , , | 1 Comment

How to run a JUnit test multiple times

I had a Spring-enabled JUnit test that I needed to run multiple times. I thought there used to be a JUnit annotation, but I couldn’t find it. I found and used the Spring @Repeat annotation. I used to set breakpoints … Continue reading

Posted in JUnit, Spring, Test Driven Development, Unit Tests | Tagged , , , , , | Leave a comment

Cucumber Spring Integration

I have a normal Spring-enabled project arranged as a Maven project. There were already JUnit tests in the project, along with the requisite Spring configuration. I needed to add Cucumber tests to the project. I started by adding a cucumber … Continue reading

Posted in Cucumber, Integration Tests, Java, JUnit, Spring, TDD, Test Driven Development, Unit Tests | Tagged , , , , , , , , , | Leave a comment

Injecting value from Spring Properties in Java

This is so easy, but I rarely use it and always forget how to use it. This is how you define a properties: The contents of the properties file is: To inject it into a class:

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

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

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

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

How to use Spring properties values in a jsp

I had a property value that contained the host of the server that I was going to be deployed on. There was more to it, because there were profiles, etc, but that is the gist of it. There is a … Continue reading

Posted in JSP, SpEl, Spring | Leave a comment