Category Archives: Java

Example of the Builder Pattern in Java

A small example of the Builder Pattern in Java. We are going to build cars with different colors, brands and models 🙂

Car.java

public class Car {
    private String brand;
    private String color;
    private String model;

    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getModel() {
        return model;
    }
    public void setModel(String model) {
        this.model = model;
    }
}

CarBuilder.java

public class CarBuilder {
    private Car car;
    
    private CarBuilder() {
        car = new Car();
    }
    public static CarBuilder aCar() {
        return new CarBuilder();
    }
    public CarBuilder withBrand(String brand) {
        car.setBrand(brand);
        return this;
    }
    public CarBuilder withColor(String color) {
        car.setColor(color);
        return this;
    }
    public CarBuilder withModel(String model) {
        car.setModel(model);
        return this;
    }
    public Car build() {
        return car;
    }
}

With these two classes above we can now build some cars:
Main.java

public class Main {
    public static void main(String[] args) {
        Car myFirstCar = CarBuilder.aCar().withBrand("Volvo")
                                          .withColor("Blue")
                                          .withModel("XC90")
                                          .build();

        Car mySecondCar = CarBuilder.aCar().withBrand("Skoda")
                                           .withColor("Grey")
                                           .withModel("130L")
                                           .build();
        System.out.println("My first car was a " + myFristCar.getBrand());
        System.out.println("My second car was a " + mySecondCar.getBrand());
    }
}

Should print:

My first car was a Volvo
My Second car was a Skoda

Tested on Ubuntu 20.04.4 LTS and Java 21

Apache Camel CXF gives you “org.apache.cxf.service.factory.ServiceConstructionException: Could not find portType named {<some namespace>}<some service>PortType”

I got this when implementing a SOAP service from a provided wsdl. I hope I would not done the same mistake if I wrote the wsdl myself, but we will never know for sure 😉

Now to the solution. You probably have something like this in your code (Apache Camel in a SpringBoot application)

...
@Component
public class CurrencyRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("cxf:bean:currencyLookupAdapterEndpoint")
            .log("Body: ${body}");
    }

    @Bean
    private CxfEndpoint currencyLookupAdapterEndpoint() {
        final CxfEndpoint cxfEndpoint = new CxfEndpoint();
        cxfEndpoint.setWsdlURL("currencies.wsdl");
        cxfEndpoint.setAddress("/getCurrencies");

        // Set the Service Class
        cxfEndpoint.setServiceClass(CurrenciesResponderService.class);

        cxfEndpoint.setProperties(new HashMap<>());
        cxfEndpoint.getProperties().put("schema-validation-enabled", "true");
        return cxfEndpoint;
    }
}

In the currencies.wsdl I had a CurrenciesResponderService and a CurrenciesResponderInterface.
If I choose the CurrenciesResponderService.class as the ServiceClass I got the error below:

org.apache.cxf.service.factory.ServiceConstructionException: Could not find portType named {<some namespace>}<some service>PortType

and if I choose the CurrenciesResponderInterface.class instead the application started without the error 🙂

Tested on Apache Camel v3.17 and SpringBoot v3.2.0

JUnit 5 assert log entries in a SpringBoot application

Asserting log entries in JUnit Jupiter is not as straight forward as one might want, but after some digging I found a way that worked for me. It should also me said that doing this i Jupiter is far easier than in previous versions of JUnit

The trick here is to use the SpringBoot OutputCaptureExtension on you test class. This can be accomplished with the @ExtendWith annotation in SpringBoot. This will inject a CapturedOutput object with output from our application

Example test class:

...    
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.containsString;

@Test
@ExtendWith(OutputCaptureExtension.class)
public void testHappyCaseSendDataToSystem(CapturedOutput output) throws Exception {

  MockEndpoint mock = camelContext.getEndpoint("mock:senddataToSystem", MockEndpoint.class);

  // Setting expectations
  mock.expectedMessageCount(1);

  // Invoking consumer
  producerTemplate.sendBody("direct:start", null);

  // Asserting mock is satisfied
  mock.assertIsSatisfied();

  // Assert log message
  assertThat(output.getOut(), containsString("LogMessage=Completed"));
}

Tested on SpringBoot v3.2.0 and JUnit 5.10.1