Mock #Component bean for Spring boot test context - mockito

I need to mock #Component beans for all my tests.
I ve tried to use a #Primary and #TestConfiguration and excludeFilter in the #ComponentScan but it does not work.
How can I put a mock bean into the ApplicationContext instead of my real bean?

Related

EntityManager thread safety and Java EE

I am new to the EJB and hibernate, and the following confuses me because there seems to be contradiction whenever i search for the definite answer.
Question:
Is it thread-safe to inject Entity manager directly into stateless bean in the following way?
#Stateless
public class SomeBean implements SomeInterface {
//..
#Inject
private EntityManager entityManager;
//... non related transaction1()
//... non related transaction2()
Would each stateless bean have its own instance of EntityManager or shared instance could be injected?
According to Hibernate docs:
An EntityManager is an inexpensive, non-thread-safe object that should be used once, for a single business process, a single unit of work, and then discarded.
Does an EJB container make it thread safe?
And according to the EJB docs, stateless session beans are inherently thread safe by not allowing different clients operate on same bean at the same time.
However, I have read examples in which EntityManagerFactory should be injected instead of EntityManager and if EntityManager is injected directly it should be done in Stateful bean.
Would it be safe to always inject EJB directly into Stateless bean like shown above or what would be use case when it wouldn't work?
Would each stateless bean have its own instance of EntityManager or shared instance could be injected ?
None of both. The #Inject (and #PersistenceContext and #EJB) injects a proxy instance, not the actual instance.
The proxy will on every method call delegate to the right available instance in the current thread and context. In other words, the container worries about this all, not you.
See also:
When using #EJB, does each managed bean get its own #EJB instance?
Why Stateless session beans?

Scope of Stateless Bean

I got a stateless bean like the following:
#Stateless
public class MyBean implements IMyBean {
#Inject
private SomeClass someClass;
#EJB
private MyRepository myRepository;
#Production
#Inject
private SomeFacade someWorker;
#PostConstruct
private void init() {
// some logic ...
}
// some more logic...
}
IMyBean is annotated with #Local.
I am running a JBoss Server. I got a .bat-file which uses MyBean. Only in the first execution of this bat-file the #PostConstruct gets called. Why is that? Which scope has MyBean? It seems like it's at least ApplicationScoped. I thought it would be RequestScope...
Your bean is an EJB before being a CDI bean. Therefore it follows the lifecycle of stateless EJB. The first time you request it, the container create it and call the #PostConstruct callback. When it's not needed anymore, it's not destroyed by returned to the EJB stateless pool, ready to be reused.
From the CDI perspective it's a #Dependent bean: it's CDI part (proxy) is recreated each time you inject it, but the EJB part is provided by the EJB container from the pool.
Looking at CDI spec, the section related to Lifecycle of stateless and singleton session beans states this regarding creation:
When the create() method of a Bean object that represents a stateless
session or singleton session bean is called, the container creates and
returns a container-specific internal local reference to the session
bean. This reference is not directly exposed to the application.
and regarding the the destruction:
When the destroy() method is called, the container simply discards
this internal reference.
Internal reference is discarded but the EJB container keep the bean for futur reuse.
If more than one user ask for this bean at the same time a new EJB might be created and the #PostConstruct will be called. So from the user point of view postConstruct calls may seem random.
The best solution is to put your stateless bean in #ApplicationScoped to avoid strange behavior.

Inject an EJB into a JSF managed bean

I have a war application with some JSF managed beans and EJB for some business logic. I'm using JSF 1.2, JBoss 5 and java 1.6
My managed bean:
#ManagedBean(name = "managedBean")
#SessionScoped
public class MyManagedBean implements Serializable {
#EJB(mappedName = "ejbBean")
public MyEjbBean ejbBean;
....
}
EJB bean:
#Singleton(name = "ejbBean")
public class MyEjbBean {
....
}
Page not rendered, error:
javax.naming.NamingException: Could not dereference object [Root exception is javax.naming.NameNotFoundException: ejb not bound]
What am I doing wrong?
JBoss 5 doesn't support #Singleton EJB (added in EJB3.1 spec), you can use the JBoss #Service annotation to create a singleton.
See the instructions here.

Spring Integration: Inject request scoped twitter-template bean into inbound-channel-adapter

I have an annotation configured request-scoped TwitterTemplate bean and I want to inject that bean into an xml configured inbound-channel-adapter.
What is the best way to do that and are there any implications that need to be considered in case of multiple sessions accessing the same inbound-channel-adapter?
Have you got any examples of twitter inbound-channel-adapter configured through java?
Inject an ApplicationContext object into your xml configured bean, as a property.
Your xml configured bean can implement InitializingBean and override the afterPropertiesSet method, where you get an instance of your twitterBean as shown in the code below.
You context gets merged irrespective of the configuration mechanism you use.
YourXmlBean implements InitializingBean {
ApplicationContext ctx;
TwitterTemplate twitBean;
...
#Override
public void afterPropertiesSet(){
twitBean = (TwitterTemplate) ctx.getBean("twitterTemplate");
}

JSF2.0 PostConstructApplicationEvent managed bean is null

We have JSF2.0 in Tomcat6.0 , need to initialize a ApplicationScope Bean while web server is started.
I tried using the PostConstructApplicationEvent processEvent method to initialize the Bean , but the managed bean from faces-config.xml is returning null.
Is there any other better way to instantiate the bean after startup?
Remove any faces-config.xml declarations related to the bean (they will otherwise override the JSF 2.0 annotations) and then annotate the bean with #ManagedBean(eager=true) as follows:
#ManagedBean(eager=true)
#ApplicationScoped
public class Bean {
// ...
}
This way the bean will always be instantiated on JSF webapp startup, without the need to view any page. You can then do the initialization job in the constructor and/or #PostConstruct of the bean.

Resources