-
1. Re: WELD-000049: Unable to invoke void com.mydomain.bc.alert.AService.init() Caused by java.lang.NullPointerException
mkouba Aug 17, 2016 10:25 AM (in response to brucespringfield)Bruce, it seems the
NullPointerException
is thrown from the application code: com.mydomain.bc.configuration.facade.AConfigurationFacadeImpl.getInputContactOfMyD(AConfigurationFacadeImpl.java:36) -
2. Re: WELD-000049: Unable to invoke void com.mydomain.bc.alert.AService.init() Caused by java.lang.NullPointerException
brucespringfield Aug 17, 2016 11:17 AM (in response to mkouba)Yes, but the Exception only occurs when I try to inject dependencies into the test with Weld. If I don't do this, this Exception doesn't occur.
-
3. Re: WELD-000049: Unable to invoke void com.mydomain.bc.alert.AService.init() Caused by java.lang.NullPointerException
mkouba Aug 18, 2016 2:17 AM (in response to brucespringfield)I'm afraid we can't help you without a deeper knowledge of the source code. E.g. it's very important to know what's on the line
AConfigurationFacadeImpl.java:36
. -
4. Re: WELD-000049: Unable to invoke void com.mydomain.bc.alert.AService.init() Caused by java.lang.NullPointerException
brucespringfield Aug 22, 2016 5:22 AM (in response to mkouba)On line 36 of AConfigurationFacadeImpl:
return configurationService.getMsr().getMsrD().stream() //
.filter(dep -> isDOfNtip(dep))//
.flatMap(asStream(MsrD::getMsrAudioFv)) //
.flatMap(asStream(MsrAudioFv::getMsrInputContact));
It seems the injected Bean is not using configurationService.
The injected bean is:
OuterEventObserverBCAC
It is being injected into the test through:
private static void initializeCdiDependencies() {
cdiContainer = CdiContainer.getInstance();
cdiContainer.initializeCdiContainer();
outerEventObserverBCAC = cdiContainer.getBean(outerEventObserverBCAC.class);
cdiContainer.shutdownCdiContainer();
}
The error starts at the line:
outerEventObserverBCAC = cdiContainer.getBean(outerEventObserverBCAC.class);
configurationService is not injected. Should it be injected also?
My cdiContainder code:
public final class CdiContainer {
private static final CdiContainer INSTANCE = new CdiContainer();
/** The logger for CdiContainer. */
// private final static Logger LOG = LogManager.getLogger(CdiContainer.class);
/**
* Get the current instance of the CdiContainer to obtain managed beans.
*
* @return the {@link CdiContainer}.
*/
public static final CdiContainer getInstance() {
return INSTANCE;
}
/**
* Constructor.
*/
private CdiContainer() {
}
/**
* Obtain the managed bean of the provided type from the CDI container.
*
* @param clazz
* the type of the bean to get from the CDI container.
* @param qualifiers
* the qualifiers for the bean selection to resolve ambiguous beans.
* @return the managed bean instance.
*/
public <T> T getBean(final Class<T> clazz, final Annotation... qualifiers) {
return weldContainer.select(clazz, qualifiers).get();
}
/**
* Initialize the CDI container. Must be only called once during application startup.
*/
public void initializeCdiContainer() {
if (weldContainer == null) {
// LOG.debug("Initializing CDI implementation JBoss Weld");
// Initialize the CDI reference implementation JBoss WELD to bootstrap CDI and get access to managed beans
final Weld weld = new Weld();
weldContainer = weld.initialize();
} else {
// LOG.debug("JBoss Weld already initialized");
}
}
public void shutdownCdiContainer() {
if (weldContainer != null) {
weldContainer.shutdown();
}
}
/** The weld container controlling the life cycle of all CDI managed beans. */
public WeldContainer weldContainer;
}
-
5. Re: WELD-000049: Unable to invoke void com.mydomain.bc.alert.AService.init() Caused by java.lang.NullPointerException
brucespringfield Aug 24, 2016 5:49 AM (in response to brucespringfield)The solution was not to inject dependencies into a Junit test with Weld. I was trying to use the WeldContainer to inject the dependencies. But there were too many nested and complex dependencies to make it realistic. I kept getting NullPointer Exceptions everywhere. So instead of using Weld and the WeldContainer, I scrapped it and used CdiRunner to run the Junit test. CdiRunner works very well injecting dependencies. It was important to use the @AdditionalClasses and @ActivatedAlternatives annotations to let CdiRunner know which classes needed to be injected. One trick was to add the test class itself into the @ActivatedAlternatives annotation.
Here is a sample:
@Alternative
@RunWith(CdiRunner.class)
@AdditionalClasses({ ACFacadeImpl.class })
@ActivatedAlternatives({ ACServiceNonArqBCT.class, MshoEventObserverBcACFake.class })
public class ACServiceNonArqBCT {
.
.
.