時何時以及爲什麼要使用Mocking,我對測試ejbs時使用模擬對象的時間和原因感到困惑。在測試EJB時測試EJB
我使用普通的JUnit,我發現它的作品找到我,但我知道它不是所有的故事。
例子:
@Stateless(name = "MyService")
public class MyBean extends BaseBean implements MyService
{
public MyBean()
{
}
public List<Category> getAllMainCategories()
{
//Category.findAll is a named query defined in Category entity
return (List<Category>) em.createNamedQuery("Category.findAll").getResultList();
}
}
而這裏的測試類:
public class MyServiceTest
{
MyService service;
@Before
public void setUp() throws Exception
{
Context context = new InitialContext();
service = (MyService) context.lookup("MyService");
}
@Test
public void getAllMainCategories() throws Exception
{
assertNotNull(service);
assertTrue(service.getAllMainCategories().size() > 0);
}
}
正如你看到的,我做了會話bean的單元測試,而不需要模擬對象......所以是這完全是真的,還是我缺乏一些東西?