Sunday, November 29, 2015

Learn REST with Spring

The popularity of REST-style APIs is not decreasing so a good knowledge in this architecture style becomes essential in many projects. In Java, there are many frameworks that you as developer can utilize in order to build a REST-style API, with Spring MVC as one of the most popular. If you wish to learn how to work effectively with REST in Spring you definitely need some good learning materials.

Saturday, November 28, 2015

Wednesday, November 25, 2015

HOW-TO: Register components using @Conditional and Condition in Spring

@Profile annotation in Spring can be used on any Spring components (e.g. @Component, @Service, @Configuration etc.) that are candidates for auto-detection. @Profile annotation accepts a single profile or a set of profiles that must be active in order to make the annotated component eligible for auto-detection. For a given @Profile({"p1", "!p2"}), registration will occur if profile p1 is active or if profile p2 is not active. The or is crucial here.

But how to achieve this with @Profile: we want to activate a given component if profile p1 is active and if profiles p2 and p3 are both inactive?

Wednesday, November 11, 2015

Thursday, October 1, 2015

Different ways of testing exceptions in Java and JUnit [Link]

Exception testing with Java and JUnit is one of the topics that got quite some attention on my blog. Therefore I gathered some of my previous articles in this topic, revised them and created a new article on my company’s blog. It is a compilation of practices for testing exceptions with Java and JUnit. Find out how to use @Test annotation with expected attribute, JUnit ExpectedException rule and AssertJ 3+ in one extensive article with lots of examples

Please read the full article here: Different ways of testing exceptions in Java and JUnit.

Tuesday, September 29, 2015

${... } placeholders support in @Value annotations in Spring

${...} placeholders in @Value annotation are used to access properties registered with @PropertySource. This is extremely useful with @Configuration beans in Spring applications, but not only. To be sure that this is possible, PropertySourcesPlaceholderConfigurer must be present in all application contexts that placeholders resolution is required.

In this blog post you will learn how to configure placeholders resolution in Spring 4 applications and how to inject different types of objects using @Value annotation, including JSR-310 Date-Time, JSR-354 Money & Currency or java.util.Optional.

Wednesday, September 16, 2015

AssertJ's SoftAssertions - do we need them?

One of the rules of writing good unit test is that it should fail for one reason, so unit test should test one logical concept. Sometime it is quite tough to have a single assertion per test. To follow the rule, we may have having multiple assertions per object in a single test.

The problem with multiple assertions in a single test though, is that if the first one fails for any reason we actually don’t know about other assertions as they will not be executed. And you know the drill: you check assertion failure reason, you fix it and re-run the test. Maybe you are lucky and the test will pass. But maybe it will fail with another assertion. With really fast unit tests this is not a big issue but when it comes to, for example, Selenium tests analysis and failure detection can become cumbersome and for sure time consuming.

Fortunately, we can re-think the way we create assertions in our tests thanks to AssertJ’s SoftAssertions.