- Create a method with a name including all attributes you want to query by in your PersonRepository interface that implements CrudRepository<> (or PagingAndSortingRepository<>)
- Create a Specification object and use Spring findAll to find all records matching the Specification.
- Use @Query in the PersonRepository and define the query using JQL
- Use GraphQL (see for example https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#core.extensions.querydsl)
Friday, June 7, 2019
Querying for Data in a Java Spring App - Alternatives
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);
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(); });
Friday, September 14, 2018
AWK - Just Examples with Explanations
Example 1
/ERROR/ { if ($0 !~ /notThisOne/) print NR $0 }
This script prints all lines with the word ERROR but without a word notThisOne. The original line number is printed in front of each line printed.
Subscribe to:
Posts (Atom)
