Embed EJB in JUnit Test Class

I defined EJB as follows:

@Stateless
@LocalBean
public class Foo {

    @Inject
    private Boo boo;

    public void doSomething() {
        boo.doOther();
    }

}

and

@Named
@RequestScope
public class Boo {

    public void doOther() {
        // I do, I do...
    }

}

I would like to add FooEJB to the JUnit test class :

import org.junit.*

public class FooTest {

    @EJB
    private Foo foo;

    @Test
    public void doSomethingTest() {
        foo.doSomething();
        assertTrue(true);
    }

}

... But this code returns NullPointerException.

What happened? How can I correctly identify this test using Java EE CDI ?

+4
source share

Source: https://habr.com/ru/post/1543072/


All Articles