0

嗨,我有一个与 MockEJB 相关的问题。我需要编写单元测试来测试调用 EJB 的代码。我使用 Mockito 为 EJB 编写模拟,并使用 MockEJB 来模拟 JNDI 上下文。

我的测试如下所示:

 @Test
 public void test1() throws Exception {
  // create a mock instance
  NetworkManager aMockManager = createMockNetworkManager();
  // deploy it in mock container and register it in JNDI
  registerMockNetworkManager(aMockManager);

  // encapsulates the JNDI lookup
  NetworkManager manager = NetworkManagerAccessor.getNetworkManager();
  // call a method
  manager.deleteObject(new TopicId(-1), null, this.userContext);
  // verify that the method was called on the mock
  verify(aMockManager).deleteObject(new TopicId(-1), null, this.userContext);
 }

 @Test
 public void test2() throws Exception {
  // create a mock instance
  NetworkManager aMockManager = createMockNetworkManager();
  // deploy it in mock container and register it in JNDI
  registerMockNetworkManager(aMockManager);

  // encapsulates the JNDI lookup
  NetworkManager manager = NetworkManagerAccessor.getNetworkManager();
  // call a method
  manager.deleteDataItem(new DataItemId(-1), null, null, null);

  // verify that the method was called on the mock
  verify(aMockManager).deleteDataItem(new DataItemId(-1), null, null, null);
 }

第一个测试运行良好,但是第二个测试系统性地失败了(mockito 说没有调用预期的方法) 在调试时,我可以看到我第二次尝试将模拟 EJB 部署到 JNDI,它没有部署,并且第一个模拟对象仍然存在。所以实际上第二个测试是从 JNDI 中提取第一个测试中创建的模拟。 另请注意,如果我单独运行第二个测试(通过评论第一个),它运行良好。

我的设置和清理方法如下所示:

 @Before
 public void setupMockJNDI() {
  try {

   // setup mockEJB
   MockContextFactory.setAsInitial();
   Context jndiContext = new InitialContext();

   // create the mock container
   mockContainer = new MockContainer( jndiContext );
  } catch (NamingException e) {
   e.printStackTrace();
  }

 }

 @After
 public void unregisterJNDI() {
  // reset mock context
  MockContextFactory.revertSetAsInitial();
 }

我真的不明白会发生什么,我的测试看起来与模拟 EJB 示例非常相似。有人有想法吗?

谢谢

4

2 回答 2

1

是的,我找到了答案……就在昨天!实际上它与 MockEJB 无关,而是与我的代码有关,即做 JNDI 查找缓存。这个电话:

NetworkManager manager = NetworkManagerAccessor.getNetworkManager();

像这样封装 JNDI 查找:

NetworkManagerHome home = (NetworkManagerHome)ServiceLocator.getInstance().getService(NetworkManagerHome.JNDI_NAME, NetworkManagerHome.class);

ServiceLocator静态缓存其 JNDI 查找,以及 JNDI 上下文:

public Object getService(String jndiName, Class className) {        
    Object narrowedService = null;
    if (cache.containsKey(jndiName)) {
        narrowedService = cache.get(jndiName);
    } else {
        // see J2EE design patterns page 197
        synchronized(this) {
            try {
                Object service = ctx.lookup(jndiName);
                narrowedService = PortableRemoteObject.narrow(service, className);
            } catch (NamingException ne) {
                throw new ServiceLocatorException("Cannot find service : "+jndiName, ne);
            }
            cache.put(jndiName, narrowedService);
        }
    }
    return narrowedService;
}

所以解决方案包括在每次测试之前重置这个缓存:

Context jndiContext = new InitialContext();
ServiceLocator.getInstance().reset(jndiContext);

// in ServiceLocator :
public void reset(Context c) {
    this.cache = new Hashtable<String, Object>();
    this.ctx = c;
}
于 2009-12-08T11:41:42.850 回答
0

在这些情况下,我发现在测试之外创建模拟并重用它更容易:

private static ServiceToMock serviceMock = mock(ServiceToMock.class);

@BeforeClass
public static void injectServiceSomewhere(){
    //...
}
@Before
public void resetServiceMock() {
    reset(serviceMock);
}

那应该这样做。

于 2010-06-14T17:53:29.550 回答