Archive

Archive for the ‘Tests’ Category

Unit testing EJBs with PowerMockito

June 23rd, 2014 No comments

I had to find a way to test a method from a stateful session bean (EJB PersonSABean implements PersonSF) defined in a service layer that calls another method from another stateful session bean (EJB HouseSFBean implements HouseSF) defined in a business layer.

To call that method from EJB HouseSFBean, the EJB PersonSABean needs to perform a lookup to get a reference to the EJB HouseSF. In that case, a static method is often created inside a static or final class. For instance :

public final class EjbUtil {
public static <T> T lookup(final Class<T> clazz) throws SomeException {
 InitialContext context = null;

try {
 context = new InitialContext(new PropertiesLoader().loadProperties("myProperties.txt"));
}
 return clazz.cast(context.lookup(name));
} catch (NamingException e) {
 throw new SomeException(e.getMessage());
} finally {
...
}

...

}
@TransactionManagement(TransactionManagementType.CONTAINER)
@Stateless(name = "PersonSA")
@Remote(value = { PersonSA.class })
@Interceptors(HandlerExceptionInterceptorSA.class)
public class PersonSABean extends BasicSABean implements PersonSA {
 ...
 private HouseSF houseSF;

 public HouseSF getHouseSF() {
    houseSF = EjbUtil.lookup(PersonSF.class, "ejb.company.project.application.HouseSF");
    return houseSF;
 }
}

But unit tests, unlike integration tests, are meant to test a specific method (or class). They are not meant to test the methods that it calls. So in my case, i do not need  to make – and should not make – a JNDI lookup to get a reference to the EJB HouseSF . So I have to mockup the EjbUtil.lookup(…)  static method.

Fortunately, PowerMockito can mock a static method so the solution is to use both Mockito and PowerMockito :

@RunWith(PowerMockRunner.class)
@PrepareForTest(EjbUtil.class)

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyListOf;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.stub;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@Category(UnitTest.class)
@RunWith(PowerMockRunner.class)
@PrepareForTest(EjbUtil.class)
public class PersonSABeanTest {

@Mock
private HouseSF houseSF;

private PersonSABean personSABean;

@Before
public void setUp() throws Exception {
  MockitoAnnotations.initMocks(this);
  personSABean = new PersonSABean();
  personSABean.setHouseSF(houseSF);
  PowerMockito.mockStatic(EjbUtil.class);
}

@Test
public void testSomeMethod() throws SomeBusinessException {
  ...
  SomeDTO someDTO = new SomeDTO();
  stub(houseSF.someMethod(anyListOf(A.class), anyListOf(BDTO.class), any(CDTO.class), anyInt())).toReturn(someDTO);
  Mockito.when(EjbUtil.lookup(anyString(), Mockito.eq(HouseSF.class), anyString())).thenReturn(houseSF);
  ...
}

Link :
https://code.google.com/p/powermock/wiki/MockitoUsage13


			
Categories: EJB, EJB 3.0, Java EE, PowerMockito, Tests Tags:

Selenium

July 25th, 2008 No comments

Today I discovered a useful testing tool, written in Javascript and HTML.

One can quickly make functional tests with the Firefox plugin Selenium IDE. If your web app runs on IE, just export the sequence of tests into either an HTML file or a Java file.

Here is a screenshot of some testing i did at work. The tests run directly inside the browser !


Categories: Selenium, Tests Tags: