I'm working on a Quarkus Extension. But I have some problems to inject CDI Beans.
In the runtime module I have this class:
#ApplicationScoped
public class CustomRestClientBuilder{
//#Produces - Removed
//#ApplicationScoped - Removed
public RestClientBuilder newBuilder(String url) throws MalformedURLException {
return RestClientBuilder.newBuilder().baseUrl(new URL(url));
}
}
In the deployment module, I have this class:
class RestClientExtensionProcessor {
private static final String FEATURE = "rest-client-extension";
#BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}
#BuildStep
public AdditionalBeanBuildItem producer() {
return new AdditionalBeanBuildItem(CustomRestClientBuilder.class);
}
}
And the problem appears in testing classes. I put the Test in Deployment module and it fails (however, in runtime module it works).
public class CustomRestClientBuilderTest {
#Inject
private CustomRestClientBuilder customRest;
#RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));
#Test
public void testGreeting() {
IDummyRestClient restClient = customRest.newBuilder("url")
.build(IDummyRestClient.class);
}
}
The error is:
Build failed due to errors
[error]: Build step io.quarkus.arc.deployment.ArcProcessor#validate threw an exception:
javax.enterprise.inject.spi.DeploymentException: javax.enterprise.inject.UnsatisfiedResolutionException:
Unsatisfied dependency for type CustomRestClientBuilder and qualifiers [#Default]
I've checked this post before:
Exposing CDI beans from Quarkus extensions
UPDATED
This way is working (adding the class I need for testing). Does it makes sense?
#RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(CustomRestClientBuilder.class,
IDummyRestClient.class));
Related
I would like to run an integration test of a single bean with resilience4j annotated method in a spring boot app. My intent is to test resiliency of bean method calls while not loading the full spring context.
The setup is as follows:
Dependencies include the following:
io.github.resilience4j:resilience4j-spring-boot2
io.github.resilience4j:resilience4j-reactor
org.springframework.boot:spring-boot-starter-aop
The resilience4j time limited spring bean with method to test:
#Service
public class FooService {
#TimeLimiter(name = "fooTimeLimiter")
public FooResponse foo() {
//entertain operation that might timeout
}
}
Configuration:
resilience4j.timelimiter.instances.fooTimeLimiter.timeoutDuration=1s
And the test:
#SpringBootTest
#ContextConfiguration(classes = FooService.class)
public class FooServiceIT {
#Autowired
private FooService service;
#MockBean
private Bar bar;
#Test
void foo_timeout() {
//setup mocks so the operation delays the output and shall end up with timeout
Assertions.assertThrows(TimeoutException.class, () -> service.foo());
}
}
However, the TimeLimiterAdvice.proceed() is not entertained, no timeout exception is thrown and the test fails.
Same question has been asked here: Testing SpringBoot with annotation-style Resilience4j but there is no solution.
I tried both approaches - implement FooService interface and program directly using the concrete class. With the same result.
How can I achieve the time limiter annotation is taken into account in my test?
Edit: I even tried plain spring test (no spring boot) with the following setup:
#ExtendWith(SpringExtension.class)
#ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class FooServiceIT {
#Configuration
#Import({TimeLimiterConfiguration.class, FallbackConfiguration.class, SpelResolverConfiguration.class})
static class ContextConfiguration {
#Bean
public FooService fooService() {
//prepare bean;
}
#Bean
public TimeLimiterConfigurationProperties timeLimiterConfigurationProperties() {
return new TimeLimiterConfigurationProperties();
}
}
#Autowired
private FooService service;
//tests...
}
Same result (i.e. no timeout exception).
When dealing with SpringBootTest and #CircuitBreaker, it was sufficient to add #EnableAspectJAutoProxy annotation to the test. After this change, the CircuitBreakerAspect was entertained and the test behaves as expected.
In order to make #TimeLimiter working as expected, one need to add #Bulkhead annotation to the method as well.
The updated method looks as follows:
#Bulkhead(name = "fooBulkhead", type = Type.THREADPOOL)
#CircuitBreaker(
name = "fooCircuitBreaker",
fallbackMethod = "fooFallback"
)
#TimeLimiter(
name = "fooTimeLimiter"
)
public CompletableFuture<FooResponse> foo() {
//...
}
and the test:
#SpringBootTest(classes = FooService.class)
#EnableAspectJAutoProxy
#Import(value = {CircuitBreakerAutoConfiguration.class, TimeLimiterAutoConfiguration.class, BulkheadAutoConfiguration.class})
public class FooServiceIT {
//...
}
Spring boot 1.5.3 project with test user-registry on H2 in memory DB
This is the Error Stacktrace
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfiguration': Unsatisfied dependency expressed through field 'myAppUserDetailsService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'SMRTUserService': Unsatisfied dependency expressed through field 'userInfoDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SMRTUserDAO': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory': Post-processing of FactoryBean's singleton object failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #2 of URL [file:/C:/temp/SMRT/target/test-classes/data.sql]: ....
Can someone help me to understand the problem? I can't solve this errors.
Test Controller
public class CustomerControllerTest extends AbstractControllerTest {
#Test
#WithMockUser(roles = "ADMIN")
public void testShow() throws Exception {
mockMvc.perform(get("/customer/list")
.contentType(APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
}
}
AbstractControllerTest
#RunWith(SpringRunner.class)
#SpringBootTest(webEnvironment = WebEnvironment.MOCK)
#AutoConfigureMockMvc
public abstract class AbstractControllerTest extends AbstractTest {
#Autowired protected MockMvc mockMvc;
#Autowired private FilterChainProxy filterChainProxy;
#Autowired private WebApplicationContext webApplicationContext;
#Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
this.mockMvc = webAppContextSetup(webApplicationContext)
.dispatchOptions(true)
.addFilters(filterChainProxy).build();
}
}
SecurityConfiguration
#Configuration
#EnableWebSecurity
#EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
#Autowired private SMRTUserService myAppUserDetailsService;
#Autowired private BCryptPasswordEncoder bCryptPasswordEncoder;
#Bean
public BCryptPasswordEncoder passwordEncoder() {
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
return bCryptPasswordEncoder;
}
#Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(myAppUserDetailsService)
.passwordEncoder(bCryptPasswordEncoder);
}
}
SMRTUSerService
#Service
#Slf4j
public class SMRTUserService implements UserDetailsService {
#Autowired private ISMRTUserDAO userInfoDAO;
#Autowired private SMRTUserRepository smrtuserRepository;
...
}
Thanks
Well, your Exception already explains pretty well what the problem is:
Failed to execute SQL script statement #2 of URL [file:/C:/temp/SMRT/target/test-classes/data.sql]: ....
I asume you're importing some test data for your tests? There must be an error in your SQL statements.
if you are using keycloak for the authentification . you might get this error .
jhipster by default gives you 8090 as auth server , so you have to change it
Solution : 1 - start your keycloak server
2 - go to your jhipster project main->ressources->config->application.yml
change issuer ui by the port where your keycloak server is running : for example : issuer-uri: http://localhost:8080/auth/realms/demo
hope that was helpfull
In a JSF 2.2 application, I want to build a war file for testing with Selenium. In that webtest.war, I want to replace a central class, called the NodeCache, with a mock version, called the WebtestNodeCache, to keep the database and other external dependencies out of the tests.
NodeCache is a managed bean:
#javax.faces.bean.ManagedBean(name = NodeCache.INSTANCE)
#javax.faces.bean.ApplicationScoped
public class NodeCache {
public static final String INSTANE = "nodecache";
// ...
}
To sneak in WebtestNodeCache, I use a ServletContextListener like this:
public class WebtestContextListener implements ServletContextListener {
#Override
public void contextInitialized(ServletContextEvent event) {
WebtestNodeCache nodeCache = new WebtestNodeCache();
ServletContext context = event.getServletContext();
context.setAttribute(NodeCache.INSTANCE, nodeCache);
}
#Override
public void contextDestroyed(ServletContextEvent sce) {}
}
In normal builds, WebtestContextListener and WebtestNodeCache are excluded from the war file, in test builds, they are included.
This seems to work: when I log in, I get dummy nodes from the WebtestNodeCache.
Is this a reliable way to replace a bean in application context or did I just get lucky?
Is there a better way to sneak in test dummies?
Using both an #ManagedBean annotation and a Listener to replace the object did not work. The code was always using the unmocked production code managed bean.
Defining a new #ManagedBean with the same name is an error and prevents deployment.
I ended up with this:
Put the #ManagedBean annotation with the same name on both the real bean and its mock.
When building, only include the mocks when building the webtest.war, but not in the regular build.
When building, have the build script (Gradle in my case) copy and filter the sources, looking for a special comment behind the #ManagedBean declaration in the production code and taking out these lines to remove the #ManagedBean declaration on the production code so that only the ones in the mock remains.
So the original NodeCache looks like this now:
#javax.faces.bean.ManagedBean(name = NodeCache.INSTANCE) // webtest:remove
#javax.faces.bean.ApplicationScoped // webtest:remove
public class NodeCache {
public static final String INSTANE = "nodecache";
// ...
}
and the mocked version has the same annotations, just without the comment:
#javax.faces.bean.ManagedBean(name = NodeCache.INSTANCE)
#javax.faces.bean.ApplicationScoped
public class WebtestNodeCache extends NodeCache {
// ...
}
Here is the relevant part of the Gradle build script:
boolean isWebtest = false
gradle.taskGraph.whenReady { taskGraph ->
isWebtest = taskGraph.hasTask(compileWebtestWarJava);
}
task copySrc(type: Copy) {
from "src"
into "${buildDir}/src"
outputs.upToDateWhen {
// Always execute this task so that resources do or don't get filtered
// when switching between normal war file and webtests.
false
}
filter { String line ->
isWebtest && line.contains("webtest:remove") ? null : line;
}
}
This solves the problem for me. Hope someone else finds it useful.
I am upgrading from JBoss 7.1.1 to WildFly 8.1.0 and can't get rid of the error described below:
14:53:04,666 ERROR [org.jboss.as.ejb3.invocation] (default task-17) JBAS014134: EJB Invocation failed on component TransRbDAO for method public java.util.List de.bss.dm.kairos.db.kairosgui.TransRbDAO.findAll(): javax.ejb.EJBException: java.lang.IllegalStateException: JBAS011048: Failed to construct component instance
TransRbDAO is:
#Stateless
public class TransRbDAO extends AbstractDAO<TransRb> {
public List<TransRb> findAll() {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<TransRb> criteria = cb.createQuery(TransRb.class);
Root<TransRb> root = criteria.from(TransRb.class);
criteria.select(root);
return em.createQuery(criteria).getResultList();
}
}
with AbstractDAO like:
public class AbstractDAO<T> {
#Inject
#CsarGuiDBExtended
#PersistenceContext(unitName = "CSAR_GUI", type = PersistenceContextType.EXTENDED)
protected EntityManager em;
public T findById(Class<T> clazz, Object primaryKey) {
T i = em.find(clazz, primaryKey);
return i;
}
}
This construct works when using only #PersistenceContext(unitName = "CSAR_GUI"), except for the expected LazyInitializationException when accessing data on the JSF-page.
The root-cause for error above is:
Caused by: javax.naming.NamingException: JBAS011878: Failed to lookup env/de.bss.dm.kairos.db.kairosgui.AbstractDAO/em [Root exception is java.lang.ArrayIndexOutOfBoundsException]
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:144)
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:81)
Is this a bug in WildFly? Was this working only because of a bug in JBoss? Or am I doing something completely wrong?
Extended persistence context can only be used in stateful session beans.
See EJB 3.2 spec section 11.11.1.1 or this article:
http://docs.jboss.org/ejb3/app-server/tutorial/extended_pc/extended.html
It appears that the failing code was implemented in JBoss 7.2.0. At this time the setting
default-extended-persistence-inheritance was introduced. Along came the Method
static Map<String, ExtendedEntityManager> More ...getCurrentCall() {
ArrayList<Map<String, ExtendedEntityManager>> stack = currentSFSBCallStack();
Map<String, ExtendedEntityManager> result = null;
if (stack != null) {
result = stack.get(stack.size() - 1);
}
return result;
}
stack.get() was throwing the ArrayIndexOutOfBounds-Exception.
When setting default-extended-persistence-inheritance="DEEP" in standalone.xml and marking TransRbDAO #Stateful the error disappears.
I'm not sure whether it was a bug in JBoss 7.1.1 that I used, or if it is a bug since 7.2.0, clarification is appreciated ;)
I am working on Eclipse and I want to create an enterprise application using Glassfish and MySQL.
I created a Enterprise Application Project, with EJB and WEB modules, named WeatherEJB and WeatherWeb.
In the WeatherEJB project I generated entities from tables, using JPA and also I created a stateless remote session bean, called CountryDAO, which implements CountryDAOBean, in order to wrap over the generated entity Country.
In the WeatherWeb project I added references to the WeatherEJB project in the Java Build bath, project references and module dependencies.
Then, in the WeatherWeb project, I created a managed bean called CountryController (at 'request' scope), which looks like this:
import javax.ejb.EJB;
import model.Country;
import service.CountryDAO;
public class CountryController
{
#EJB
CountryDAO countryDao;
private Country country;
public CountryController()
{
country = new Country();
}
public String saveCountry()
{
String returnValue = "success";
try
{
countryDao.saveCountry(country);
}
catch (Exception e){
e.printStackTrace();
returnValue = "failure";
}
return returnValue;
}
public Country getCountry(){
return country;
}
public void setCountry(Country country){
this.country = country;
}
}
Although I can deploy successfully the application on Glassfish, when I try to access a jsf that uses the CountryController, I get the following errors:
type Exception report
message
descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: javax.faces.FacesException: com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref managedBeans.CountryController/countryDao#jndi: service.CountryDAO#null#service.CountryDAO#Session#null into class managedBeans.CountryController
root cause
javax.faces.FacesException: javax.faces.FacesException: com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref managedBeans.CountryController/countryDao#jndi: service.CountryDAO#null#service.CountryDAO#Session#null into class managedBeans.CountryController
root cause
javax.faces.FacesException: com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref managedBeans.CountryController/countryDao#jndi: service.CountryDAO#null#service.CountryDAO#Session#null into class managedBeans.CountryController
root cause
com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref managedBeans.CountryController/countryDao#jndi: service.CountryDAO#null#service.CountryDAO#Session#null into class managedBeans.CountryController
root cause
javax.naming.NameNotFoundException: service.CountryDAO#service.CountryDAO not found
What am I missing? or what am I doing wrong?
Actually, instead of the implementation class:
#EJB
CountryDAO countryDao;
I should have used the interface:
#EJB
CountryDAOBean countryDao;