Capturing arguments with mockito

EncryptedFileInfo info = cut.storeEncryptedFile(encryptionSecret, plainContents, fileName);

ArgumentCaptor<byte[]> argument = ArgumentCaptor.forClass(byte[].class);

verify(cut).createFileAndAdd(eq(fileName), argument.capture());
byte[] encryptedContent = argument.getAllValues().get(0);

Where intercepted method is:

void createFileAndAdd(String fileName, byte[] encryptedContents);

Bootstraping Spring core using the context

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

At this point al found beans has been registered and can be used. As an example code would go further as follows, taking care that ExStateMachine.class  is annotated as @Component :

ExStateMachine machine = context.getBeanFactory().getBean(ExStateMachine.class);


There are several available conext, all implementing the interface:


package org.springframework.context.annotation:


package org.springframework.jca.context:

Apprenders and formating a default java.util.logger

java.util.logging.FileHandler.level = INFO
java.util.logging.FileHandler.pattern = log.%u.%g.txt
java.util.logging.SimpleFormatter.format= %1$tl:%1$tM:%1$tS %1$Tp %2$s %4$s: %5$s%n

More info about SimpleFormatter:

More Info about formatting params:

Pass parameters to java main method using graddle

To pass for example a param “-a” with value “” to the static void main method that tha graddle task will run,  you will need to :

  • add following entry to file, or assert that it is already there:
    run {
        if (project.hasProperty("appArgs")) {
            args =
  • run graddle on the following form:
    gradle  run  -PappArgs="['-a',  '']"

Create a correct 201 Created response with JAX-RS

public Response createProject(@Context UriInfo uriInfo, JsonObject json) {
        Project project = projectConverter.fromJson(json);
        project = em.merge(project);

        URI uri = uriInfo.getBaseUriBuilder()
                .resolveTemplate(PathExpressions.workspaceId, getCurrentWorkspace().getId())
                .resolveTemplate(PathExpressions.projectId, project.getId())

        return Response.created(uri).build();
The @Context UriInfo uriInfo provides information about the current URI. 
The .path(ProjectResource.class)  call will return the path used for the ProjectResource.class. 
The .resolveTemplate("{workspaceId:\\d+}", getCurrentWorkspace().getId())  will replace the workspaceId template variable for the actual wokspace id.

Once the whole path has been created, it is only needed to put it into a created response.

Collect into Jsonp JsonArray using without using foreach

Each collector has three parts:

  • A supplier: provides with instances of the accumulator.
  • An accumulator: accumulates the objects being collected. Several instances of accumulator can be used.
  • A combiner: combines all the accumulator putting all collected objects together.

For the JsonArray the combiner, accumulator  and combiener are respectively:

JsonArrayBuilder createArrayBuilder()
JsonArrayBuilder add(JsonValue value)
JsonArrayBuilder add(JsonArrayBuilder builder)

    public JsonArray getArray(Jsonable[] objects) {
        return Stream.of(objects).map(Jsonable::toJson)


    public static class Jsonable {

        public JsonObject toJson() {
            return Json.createObjectBuilder().add("someId",;

Get java.util.logging working on UnitTesting

If you need to active the java.util.logging on your test, you can achieve it just adding the vm option

where can be something like

handlers = java.util.logging.ConsoleHandler
your.package.level = FINE
java.util.logging.ConsoleHandler.level = FINE

You can find a more complete example on