Is the usage of lombok's @RequiredArgsConstructor good or bad in @QuarkusTest integration tests? #52560
Replies: 1 comment
-
|
Great question! The short answer: avoid Why it's problematicQuarkus uses CDI (Contexts and Dependency Injection) under the hood. CDI has specific rules about how beans are instantiated:
Best practice@QuarkusTest
class MyServiceTest {
@Inject
MyService myService; // ✅ Standard Quarkus way
@InjectMock
ExternalClient client; // ✅ For mocks
@Test
void shouldDoSomething() {
// test code
}
}In production code (non-test)For application beans, constructor injection IS recommended, but use @ApplicationScoped
public class MyService {
private final MyRepository repo;
@Inject // explicit — no Lombok needed
public MyService(MyRepository repo) {
this.repo = repo;
}
}Or if you really want Lombok, combine it with @ApplicationScoped
@RequiredArgsConstructor(onConstructor_ = @Inject)
public class MyService {
private final MyRepository repo;
}But note that Quarkus (unlike Spring) does not auto-detect single-constructor injection — you always need TL;DR: Use |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I wonder if there is a reason that @requiredargsconstructor should be avoided or not. What is best practice and why?
Beta Was this translation helpful? Give feedback.
All reactions