user1433804
user1433804

Reputation: 657

how to Mockito unit test with Session Beans

I am learning to write a UnitTest for my JSF project which has ManangedBeans and Session Beans

I have a problem invoking the EJB from Mockito test

package Test;

import ejb.CountrySession;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.faces.view.ViewScoped;

@Named(value = "countryMB")
@ViewScoped
public class CountryMB implements Serializable {

@EJB
private CountrySession countSession;
//
private String countryName;
//
private StatusMsg msg;

public CountryMB() {
}

public void setMsg(StatusMsg msg) {
    this.msg = msg;
}

public void setCountSession(CountrySession countSession) {
    this.countSession = countSession;
}

public String getCountryName() {
    return countryName;
}

public void setCountryName(String countryName) {
    this.countryName = countryName;
}

public void ajaxAll() {

}

public void saveCountry() {
    if (countryName != null && !countryName.trim().isEmpty()) {
        boolean chk = countSession.chkCountry(countryName);
        if (!chk) {
            chk = countSession.addCountry(countryName);
            if (chk) {
                msg.addInfo("Add Country", "New Country added");
            } else {
                msg.addError("Add Country", "Unable to add Country");
            }
        } else {
            msg.addWarn("Add Country", "Country already exists");
        }
    } else {
        msg.addWarn("Add Country", "Required parameter not available");
    }
}

}

Now in my Test Code i have the following

package Test;

import ejb.CountrySession;
import entities.Country;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class CountryMBTest {

@Mock
private CountryMB countryMB;
@Mock
private StatusMsg sm;

@Mock
private CountrySession countSession;

@Mock
private EntityManager em;

@Mock
private Query qry;

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    countryMB = new CountryMB();
    countryMB.setMsg(sm);
    countryMB.setCountSession(countSession);
}

@After
public void after() {
    countryMB = null;
}

@Test
public void infoCountrySave() {
    countryMB.setCountryName("Test");
    countryMB.saveCountry();
    verify(sm).addInfo("Add Country", "New Country added");
}

@Test
public void errorCountrySave() {
    countryMB.setCountryName("Test");
    countryMB.saveCountry();
    verify(sm).addError("Add Country", "Unable to add Country");
}

@Test
public void warnCountrySave() {
    countryMB.setCountryName("Test");
    countryMB.saveCountry();
    verify(sm).addWarn("Add Country", "Country already exists");
}

@Test
public void chkCountSave() {
    List<Country> countLst = null;
    Country dum = mock(Country.class);
    EntityManager em = mock(EntityManager.class);
    Mockito.when(em.find(Country.class, 111)).thenReturn(dum);
    CountrySession cs = Mockito.mock(CountrySession.class);
    Mockito.when(cs.chkCountry("Test")).thenCallRealMethod();
    Mockito.when(cs.getEm()).thenReturn(em);
    String name = "Test";
    Assert.assertNull(cs.chkCountry(name));
}

}

My table has only one Record pk=1, Country=Test

The above test code never check the session beans, it just throw

java.lang.NullPointerException
at ejb.CountrySession.chkCountry(CountrySession.java:67)
at Test.CountryMBTest.chkCountSave(CountryMBTest.java:112)

And for infoCountrySave() & warnCountrySave() it just never check the supplied value in the database.

As i have never used any UnitTest earlier so i really don't know if what i am doing is correct, moreover i could not figure out any working code by googling.

It will be of great help if anyone can guide me to some resource available online or even let me know what is that i need to correct to get the above mockito test work with the ejb part.

Upvotes: 0

Views: 2823

Answers (1)

kakashi hatake
kakashi hatake

Reputation: 1185

Change @Mock with @InjectMocks for

private CountryMB countryMB 

and when you get java.lang.NullPointerException. Mostly you miss inject some class or some dependency on by called when

Upvotes: 0

Related Questions