Friday, June 28, 2019

Building Java Applications with Gradle

Gradle wrapper:

  • In order to make sure you build the project with the same version of gradle as other developers (which can make a big difference), build the application using the wrapper: ./gradlew
    It will start with downloading the right gradle version, unless it's already there.
  • Watch out for running old gradlew for a version like 2.3 with a new java like version 11.  It quits silently without doing anything.

Useful commands:

  • gradle build -x test  -- run build but skip tests

Friday, June 7, 2019

Querying for Data in a Java Spring App - Alternatives


  1. Create a method with a name including all attributes you want to query by in your PersonRepository interface that implements CrudRepository<> (or PagingAndSortingRepository<>)
  2. Create a Specification object and use Spring findAll to find all records matching the Specification.
  3. Use @Query in the PersonRepository and define the query using JQL
  4. Use GraphQL (see for example https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#core.extensions.querydsl)

Monday, June 3, 2019

Unit testing with jUnit5 and Mockito

Mocking for Spring injected dependencies

import org.junit.jupiter.api.Test;

@ExtendWith(MockitoExtension.class)
class Person {
   @Mock
   DependencyA dependencyA;

   // Don't use @InjectMocks if using constructor injection with final dependencies.
   // Mockito can't set the new mocks for each test
   TestedClass target;

   @BeforeEach
   public void setup() {
      // only needed if you do Mockito.when(dependencyA.methodAbc()).thenReturn() or similar
      MockitoAnnotations.initialize(this);  
      target = new TestedClass(dependencyA);
   }

   @Test
   public void testMethodWhatsyourname() {
      // given
      ...
      
      // when
      target.whatsYourName();

      // then 
      ...
   }
}

Monday, May 20, 2019

Java Optional - Good Practice



  • orElseThrow: if personService.getAll() returns an Optional, then:
    return personService.getAll().map(converter::toResource).orElseThrow(
           () -> new NoResourceFound(...)
    );
  • orElse: if personService.getByLastName() returns an optional:
    return personService.getByLastName(lName).orElse(null);

Tuesday, April 30, 2019

Java Streams

Null elements in the stream

I thought, I saw a statement that Java streams filters out the null elements in the stream but the information must have been wrong.  So, if nulls are expected, or just in case, do:

... .filter(Objects::nonNull). ....

Monday, April 29, 2019

Java 9 - What's New In It


Modules

  • defined by file module-info.java in the module root directory
  • defines the module name, which package the module exports and
    what other modules it requires
  • if package org.sjb exports to a module, its subpackage isn't
    exported automatically; it can be exported by any module
  • each package is exported by a single module
  • jlink - packages a modules with all its required modules and the needed parts of the JRE into a standalone app which won't need Java to be installed where it runs
  • unnamed modules - all not defined stuff gets packed into a single unnamed module.  It exports all packages but only unnamed modules can call them.
  • automatic modules ...

Services


Using modules, the client code can receive all available implementations
  of an interface, and choose as one it needs (server and client are in the same JVM;
  it's not about http clients)

Monday, March 18, 2019

jUnit Fun

jUnit and Mockito Fun

More Interesting Elements of Unit Testing and Mocking


  • Verify that a method was called on a mock

    • Verify the parameters the method was called with
  • Return a custom response from a method called on a mock, response based on the parameters of the call
    Mockito.when(myMock.save(any(Person.class))).thenAnswer(i -> {
        Person person = (MyClass) i.getArguments()[0];
        return "Hello, " + person.getFirstName();
    });