gpt4 book ai didi

java - 使用 Mockito、TestNG 和 OpenEJB 对 EJB 进行单元测试

转载 作者:行者123 更新时间:2023-11-30 08:10:57 26 4
gpt4 key购买 nike

我有以下 EJB:

PersonService.java

@Local
public interface PersonService {
long countPersons();
}

PersonServiceImpl.java

@Stateless
public class PersonServiceImpl implements PersonService {

@EJB
private RemotePersonService remotePersonService;

@Override
public long countPersons() {
return remotePersonService.getAllPersons().size();
}
}

RemotePersonService.java

@Local
public interface RemotePersonService {
List<Person> getAllPersons();
}

RemotePersonServiceImpl.Java

@Stateless
public class RemotePersonServiceImpl {
@Override
public List<Person> getAllPersons() {
// Here, I normally call a remote webservice, but this is for the purpose of this question
List<Person> results = new ArrayList<Person>();
results.add(new Person("John"));
return results;
}
}

这是我的测试

AbstractTest.java

public abstract class AbstractTest {

private InitialContext context;

@BeforeClass(alwaysRun = true)
public void setUp() throws Exception {
System.setProperty("java.naming.factory.initial", "org.apache.openejb.client.LocalInitialContextFactory");

Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("/unittest-jndi.properties"));

context = new InitialContext(properties);
context.bind("inject", this);

}

@AfterClass(alwaysRun = true)
public void tearDown() throws Exception {
if (context != null) {
context.close();
}
}
}

PersonServiceTest.java

@LocalClient
public class PersonServiceTest extends AbstractTest {

@EJB
private PersonService personService;

@Test
public void testPersonService() {
long count = personService.countPersons();

Assert.assertEquals(count, 1l);
}
}

现在,我想做的是用 Mockito 的模拟替换 PersonServiceImpl.java 中的 RemotePersonService 实现,并且在我的 testPersonService 方法中仍然有相同的调用。

我试过了:

PersonServiceTest.java

@LocalClient
public class PersonServiceTest extends AbstractTest {

@Mock
private RemotePersonService remotePersonService;

@EJB
@InjectMocks
private PersonService personService;

@BeforeMethod(alwaysRun = true)
public void setUpMocks() {
MockitoAnnotations.initMocks(this);

List<Person> customResults = new ArrayList<Person>();
customResults.add(new Person("Alice"));
customResults.add(new Person("Bob"));

Mockito.when(remotePersonService.getAllPersons()).thenReturn(customResults);
}

@Test
public void testPersonService() {
long count = personService.countPersons();

Assert.assertEquals(count, 2l);
}
}

但这行不通。 PersonService中没有注入(inject)@Mock RemotePersonService,依然使用真正的EJB。

我怎样才能使它工作?

最佳答案

不要在测试中使用注解。有一个构造函数可以连接所有依赖项。

@Stateless
public class PersonServiceImpl implements PersonService {

@EJB
private RemotePersonService remotePersonService;

// Let your test instantiate a mock service and wire it into your test instance using this constructor.
public PersonServiceImpl(RemotePersonService rps) {
this.remotePersonService = rps;
}

@Override
public long countPersons() {
return remotePersonService.getAllPersons().size();
}
}

创建模拟并将它们传递给它。您的测试可能如下所示:

@LocalClient
public class PersonServiceTest extends AbstractTest {

@Test
public void testPersonService() {
RemotePersonService mockRemotePersonService = Mockito.mock(RemotePersonService.class);
List<Person> customResults = new ArrayList<Person>();
customResults.add(new Person("Alice"));
customResults.add(new Person("Bob"));
Mockito.when(mockRemotePersonService.getAllPersons()).thenReturn(customResults);
PersonService personService = new PersonServiceImpl(mockRemotePersonService);
long count = personService.countPersons();
Assert.assertEquals(count, 2l);
}
}

关于java - 使用 Mockito、TestNG 和 OpenEJB 对 EJB 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31403973/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com