Java

Java 8 StringJoiner

Posted on by  
Sjoerd Schunselaar

At the release of Java 8 the most attention went to the Lamda’s, the new Date API and the Nashorn Javascript engine. In the shade of these, there are smaller but also interesting changes. Amongst them is the introduction of a StringJoiner. The StringJoiner is a utility to delimit a list of characters or strings. You may recognize the code below:

String getString(List<String> items)
    StringBuilder sb = new StringBuilder();
    for(String item : items) {
        if(sb.length != 0) {
            sb.append(",");
        }
        sb.append(item);
    }
    return sb.toString();
}

Continue reading →

Wicket quick tips: create a download link

Posted on by  
Sjoerd Schunselaar

Apache Wicket is a populair web framework. There are a many reasons why I like to use Wicket, for instance: it offers a great mark-up/logic separation and using Wicket it’s very easy to implement AJAX functionality without writing one line of Javascript.

To provide you with simple and short tips and tricks for Wicket I write this series of blogs. In this first blog of the series I will show you how to create a download link in several ways.

Continue reading →

Nifty JUnit : How to test for an exception

Posted on by  
Willem Cheizoo

Testing for exceptions in JUnit is something we have to deal with! We want to test if an exception occurs in a particular situation, or even if the exception contains a particular message. The question is: How to test for an exception in Junit? What we see very often is this:

import org.junit.Test;

public class JDrivenServiceTest {

    JDrivenService service = new JDrivenService();

    @Test(expected = ArticleNotFoundException.class)
    public void testPublishArticle_WithException() throws Exception {
        service.publishArticle(null);
    }
}

Continue reading →

Using ArgumentMatchers with Mockito

Posted on by  
Albert van Veen

Mockito is a mock framework which you can use to write simple and clean tests. One of it’s nice features is the ArgumentMatcher. With the ArgumentMatcher you can determine the outcome of your mocked service or repository based on any condition you want. Imagine we have a CandyService which can get a list of candies which are liked by a specific customer. This service uses the external ChocalateService which specifically checks if the customer likes chocolate. The CandyServiceTest class looks like this:

@RunWith(MockitoJUnitRunner.class) // will initiate and inject the mocks

public class CandyServiceTest {

    @Mock
    private ChocolateService chocolateService;

    @InjectMocks
    private CandyService candyService = new CandyServiceImpl();

    @Test
    public void testCustomerLikesChocolate() throws ParseException {
        Customer customer = new Customer();
        customer.setFirstName("Albert");
        List candiesLikedByCustomer = candyService.getCandiesLikeByCustomer(customer);
        assertTrue(candiesLikedByCustomer.contains(Candy.CHOCOLATE), "Expected customer to like chocolate");

    }

    @Test
    public void testCustomerWhoNotLikesChocolate() throws ParseException {
        Customer customer = new Customer();
        customer.setFirstName("Any other firstname");
        List candiesLikedByCustomer = candyService.getCandiesLikedByCustomer(customer);
        assertFalse(candiesLikedByCustomer.contains(Candy.CHOCOLATE), "Expected customer not to like chocolate");

    }
}

Continue reading →

Auto inject dependencies in JUnit using Mockito

Posted on by  
Sjoerd Schunselaar

While writing unit tests, you often have to mock dependencies like services or controllers. Often  a constructor is used to autowire the dependencies as shown in the example below. In the Test class I instantiated the ContactService using a contactRepository Mock object

@Service
public class ContactServiceImpl implements ContactService {

private final ContactRepository contactRepository;

    @Autowired
    public ContactServiceImpl(final ContactRepository contactRepository) {
    this.contactRepository = contactRepository;
    }

    public void saveContact(final Contact contact) {
    contactRepository.save(contact);
    }
}

Continue reading →

Consume REST JSON webservices easily using Spring Web!

Posted on by  
Sjoerd Schunselaar

Spring made it very easy to consume JSON webservices. In this article I describe how to receive and parse JSON and how to send your Java objects as JSON. First we need to include the required dependencies. If you use maven, include the following dependencies:

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>3.1.2.RELEASE</version>
</dependency>
<dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>1.9.9</version>
</dependency>

Continue reading →

shadow-left