Spring Boot Integration Testing with @SpringBootTest and H2
Test a Spring Boot 4.1.2 REST API with @SpringBootTest and H2. Compare real HTTP and MockMvc tests, seed data with @Sql, and mock only the notification boundary.
Test a Spring Boot 4.1.2 REST API with @SpringBootTest and H2. Compare real HTTP and MockMvc tests, seed data with @Sql, and mock only the notification boundary.
Test a Spring Boot REST controller with JUnit and Mockito. Compare direct method tests, standalone MockMvc and @WebMvcTest using verified GET, POST and 404 examples.
The @SpringBootTest annotation in Spring Boot allows us to create integration tests by loading the entire application context. It acts as a bridge between the application configuration and the test framework so that tests can verify the behavior of your application in an environment similar to production.
Use Spring Boot @TestConfiguration to add fixed test beans without losing production wiring. Compare nested and imported configurations in a runnable Boot 4.1.2 example.
Learn to include JUnit 5 in a Spring boot application and write unit tests. Check out a demo of JUnit 5 tests for Spring Boot REST controller.
Learn to configure timeouts in android apps using retrofit 2 and OkHttp library. We will learn to configure default timeouts and custom connection timeouts in this tutorial.
In this Retrofit2 tutorial, we will learn to parse RSS feeds in an Android application using Retrofit2 and the SimpleXmlConverterFactory.
Learn to parse a sitemap in an Android app using Retrofit2, which uses XML parsing with a simple XML converter dependency. Here’s a Retrofit sitemap reader example.
Learn to execute synchronous and asynchronous calls (i.e. blocking and non-blocking calls) in an android app using Retrofit 2 and OkHttp library.
Though we can easily create Retrofit instances on demand for different client in place, having a dedicated factory method or service generator method is definitely desirable. In this service generator, we can have centralized place to add/modify the authentication, logging and error handling logic. 1. Retrofit service generator A service …
Learn to add logging support in Retrofit 2 using HttpLoggingInterceptor and OkHttpClient APIs. In Retrofit 2, all network operations are performed via OkHttp library. OkHttp provides HttpLoggingInterceptor which logs HTTP request and response data. An example to add HttpLoggingInterceptor to OkHttpClient. 1. Dependency 2. Create and add HttpLoggingInterceptor we can …
Retrofit.Builder class uses the Builder API to allow defining the URL end point for the HTTP operations and finally build a new Retrofit instance.
Learn to perform requests with single, multiple and even optional query parameters and path parameters using @Query and @Path annotations in Retrofit 2.
In this Retrofit 2 tutorial, we will learn the basics of Retrofit and then we will create a declarative REST client for Android for executing HTTP requests against a REST API. 1. What is Retrofit? Retrofit was developed by Square and in its documentation, it is a type-safe REST client …
Use Gson with Spring Boot 4.1.2. Compare an injected Gson bean with MVC JSON responses, customize LocalDate, and select Gson for the message converter.
Learn to use custom serialization and deserialization using two interfaces Gson JsonSerializer and JsonDeserializer to convert between json and java object.
Gson JsonParser is used to parse Json data into a parse tree of JsonElement and thus JsonObject, which can be used to get JSON values using their keys.
Gson allows us to exclude or ignore the fields from Java classes using @Expose annotation, exclude fields with modifiers and custom exclusion strategies.
In Gson, multiple versions of the same object can be maintained by using @Since annotation. It takes single parameter – ignoreVersionsAfter.
Learn to work with Gson JsonReader class which is a pull-based streaming JSON parser. It helps in reading a JSON as a stream of tokens. 1. JsonReader Read More : Streaming parser of XML 2. Tokens In streaming mode, every JSON data is considered an individual token. When we use …
Learn Gson with examples. Convert Java objects, lists and maps to JSON and back with toJson() and fromJson(), configure GsonBuilder, use @SerializedName and @Expose, read JSON with JsonParser and write a custom TypeAdapter.
The default behavior in Gson is that null fields are ignored. For example, if in Employee object, we do not specify the email (i.e. email is null) then the email will not be part of serialized JSON output. Gson ignores null fields because this behavior allows a more compact JSON …
By default, Gson prints the JSON in a compact format. It means there will not be any whitespace in between field names and their value, object fields, and objects within arrays in the JSON output etc. 1. GsonBuilder.setPrettyPrinting() To enable the Gson Pretty Print feature, we must configure the Gson …
To customize the behavior of Gson, use GsonBuilder to create a new customized Gson instance. e.g. pretty printing, FieldNamingPolicy, ExclusionStrategy etc.
Gson @SerializedName is used to change the name of fields between JSON and Java objects while the serialization and deserialization process.
Learn to use Google GSON library to deserialize or parse JSON to Set (e.g. HashSet) in java. Also, learn to serialize Set to JSON.
Learn to use Google GSON library to deserialize or parse a JSON array to a Java array or List object. It’s worth mentioning that JSON has only array datatype. Java has both – arrays and lists. 1. Parsing JSON Array as Root To parse JSON, with array as root, we …
Generate a Java UUID with randomUUID(), build name-based v3 and v5 UUIDs, create time-ordered v7 keys on Java 25 and 26, and parse and store them safely.
Pair in Java with Map.entry(), AbstractMap.SimpleEntry, records, javafx.util.Pair, Commons Lang Pair and Vavr Tuple2, plus a guide to choose one.
Read a classpath file in Spring Boot with Resource, ClassPathResource, or ResourceLoader. See why streams work in a packaged JAR when getFile() does not.
At a high level, @RestController is a convenient annotation for writing REST APIs and combines @Controller and @ResponseBody annotations.
Turn off the Spring Boot startup banner with properties, a launch argument, or SpringApplication. See verified Spring Boot 4.1.2 output and how overrides work.
Understand Spring Boot 4 application and auto-configuration annotations with a runnable example of property conditions, missing beans, and three outcomes.
Learn to parse a date-time string to UTC in Java with Instant, OffsetDateTime and ZonedDateTime. Covers strings with Z, an offset, GMT or no zone at all. Also covers RFC 1123 dates, old SimpleDateFormat code and common DateTimeParseException causes.
ResolverStyle.STRICT rejects dates like February 30 that SMART and LENIENT adjust. See all three styles side by side, the uuuu vs yyyy trap and fixes.
Give each Spring Boot profile its own log levels, files and format with application-{profile}.properties or springProfile blocks in logback-spring.xml, including JSON logs only in production.
Learn to use and configure logging to console in spring boot application. Also learn to configure and use console appenders available in logback and log4j2.
Learn to create multiple log files in spring boot applications using multiple file appenders. Learn to configure the appenders wiih log4j2 and logback.
Project lombok can also be used to configure logging in spring boot applications and thus removing the boilerplate code for getting the logger instance.
Logging in spring boot is very easy to configure. Learn to support the logging providers such as commons logback, log4j2 and commons logging in Spring boot.
Learn to create aspectj based interceptor in spring boot application to write performance logging based on time taken in method executions with example.
Learn to configure Ehcahce 3 with Spring Boot 2.x and Spring Boot 3.x applications, use @Cacheable annotation and log caching events.a
Count vowels and consonants in Java with a loop, switch, streams and regex, plus y, digits, accents and locale pitfalls, with tested examples.
Reverse words in a string in Java with split() and join, List.reversed(), a loop, streams, two pointers and StringUtils, with extra spaces and punctuation.
Sort a String array alphabetically in Java with Arrays.sort, ignore case, reverse it, use Collator for accents, and handle null elements.
Regex match any character in Java with the dot, .* and .+, plus DOTALL and [\s\S] for line breaks, lazy .*?, literal dots and character classes.
Regex end of line and start of line in Java with $ and ^, MULTILINE mode, the trailing line break trap, \A and \z, and Windows line breaks with \R.
Swap two numbers in Java with a temp variable, arithmetic, XOR or one line, and learn where each trick fails and why a swap method cannot work.
Find prime numbers from 2 to N in Java with the Sieve of Eratosthenes, check one number fast, get the first N primes and test big numbers.
Learn to have optional @PathVariable valuein spring mvc application when required attribute to set to false or expect variable’s value to null.
HowToDoInJava provides tutorials and how-to guides on Java and related technologies.
It also shares the best practices, algorithms & solutions and frequently asked interview questions.