Top Micronaut Interview Questions (2024)
What is Micronaut?
Is Micronaut asynchronous?
What distinguishes Micronaut from Spring?
What command-line tool does Micronaut use?
How to create a Micronaut application using mn command?
How to return a single object asynchronously in micronaut application?
What are the Micronaut beans scopes?
How to test Micronaut application code using Junit?
Q: What is Micronaut?
Ans:
Micronaut is an open source JVM framework for developing services using Java, Kotlin or Groovy. Micronaut is a new JVM framework for developing microservices using Java, Kotlin or Groovy. It stands out due to its small memory footprint and quick startup time.
Q: Is Micronaut asynchronous?
Ans:
Micronaut will serve HTTP requests in a non-blocking event-loop if used with Netty, allowing your application to accept (and scale) requests beyond what a standard Servlet (2.5 ~ event 3.1 with Async requests) based container could allow.
Take a look at our suggested posts:
Q: What distinguishes Micronaut from Spring?
Ans:
Spring follows a different direction than Micronaut. The startup time, in particular, is significantly lowered, allowing Java developers to enter the serverless world. In addition, the amount of RAM used lowers.
The overhead does increase the start time during compilation, despite the fact that this is a fairly straightforward and tested solution. Spring uses Reflection to scan the classpath for beans, initialises them, and then dynamically loads them into the application context at runtime. Micronaut, on the other hand, uses annotation processors, which gather the relevant data at compile time and perform the required dependency injection (DI) and aspect-oriented programming (AOP) transformations ahead of time (AOT).Q: What command-line tool does Micronaut use?
Ans:
Micronaut comes with a useful command-line tool called mn
.
$ mn --version
Micronaut Version: 1.2.6
JVM Version: 1.8.0_201
Q: How to create a Micronaut application using mn command?
Ans:
It uses the Gradle build tool to construct a new application (whenever needed, we can switch to Maven.). We want to utilize Java (--lang=java) and Spock testing framework (--features=spock) rather than JUnit.
$ mn create-app micronaut-nonblocking-async-example--lang=java --features=spock
Generating Java project...
Application created at /techgeeknext/micronaut-nonblocking-async-example
Q: How to return a single object asynchronously in micronaut application?
Ans:
Maybe
return type indicates that it returns a single Employee, no value, or throws an
exception.
import io.reactivex.Maybe;
import io.reactivex.schedulers.Schedulers;
.....
.....
public Maybe<Employee> findEmployeeById(final String id) {
return Maybe.just(id)
.subscribeOn(Schedulers.io())
.map(it -> employee.getOrDefault(it, () -> null).get());
}
SubscribeOn(Schedulers.io())
sends the calculation to a scheduler that handles IO-bound
work.
Q: What are the Micronaut beans scopes?
Ans:
Micronaut have 6 built-in scopes for beans. Additional scopes can be defined by declaring a
@Singleton
bean that implements the CustomScope interface, as per JSR-330. The
following is a list of built-in scopes:
- Singleton Scope: It provides Singleton scope for a bean.
- Prototype Scope: :This is a default scope for bean. When it's injected, a new instance is created every time.
- ThreadLocal Scope: It is a Custom scope uses
ThreadLocal
to associates a bean per thread. - Context Scope: In this scope, a bean is created at the same time as the ApplicationContext.
- Infrastructure Scope: the
@Context
bean cannot be updated or replaced. - Refreshable Scope: It is a custom scope, which allow beans state to be refreshed
using
/refresh
endpoint
Q: How to test Micronaut application code using Junit?
Ans:
Micronaut and JUnit 5 make testing simple. We need to add micronaut-test-junit5
dependency in the pom.xml file. Now all we have to do is add the @MicronautTest
annotation to the test class.
@MicronautTest
public class TestsMicronautRest {
@Inject
ExampleOneService firstService;
@Inject
ExampleTwoService secondService;
@Test
public void testExample() {
............
............
}