这是我的源代码。
public class Config{
private Long id;
private ConfigService configService;
public Config(Long id){
this.id=id;
}
public setConfigService(ConfigService configService){
this.configService=configService;
}
public ConfigService getConfigService(){
return configService;
}
public void callingService(){
ManagedElInterface mei = null
mei=getConfigService().getIdsFromConfigService(getId());
//...
}
private Long getId(){
return id;
}
}
我嘲笑
mei=getConfigService().getIdsFromConfigService(getId());
但是我可以在“ mei”中看到null
下面是我使用EasyMock进行模拟的测试类。
public class TestConfig{
@Test
public void testcallingService{
Config config=new Config(12345l);
ConfigService configService =EasyMock.createMock(ConfigService .class);
config.setConfigService (ConfigService );
ManagedElInterface mei=new ManagedElInterface();
//here i mocked
EasyMock.expect(configService.
getIdsFromConfigService(EasyMock.anyLong()))
.andReturn(mei);
config.callingService();
}
}
ConfigService类:
public class ConfigService{
public ManagedElInterface getIdsFromConfigService(Long l){
//database code
return new ManagedElInterface();
}
}
ManagedElInterface类:
public class ManagedElInterface{
//data
}
这里 ConfigService 是具有 getIdsFromConfigService(Long l)的类。我不想执行此方法,所以我想进行模拟。 getConfigService()。getIdsFromConfigService(getId()); 返回 ManagedElInterface 类的对象,因此我为 ManagedElInterface创建对象并返回自己的数据 。 你能提供任何线索我在做什么错。 预先感谢
答案 0 :(得分:1)
ConfigService:
public class ConfigService {
public String getId(Long id) {
return "hardcoded";
}
}
配置
public class Config {
private Long id;
private ConfigService service;
public Config(Long id) {
this.id = id;
}
public void setConfigService(ConfigService configService){
this.service=configService;
}
public String callingService() {
String result = this.service.getId(id);
System.out.println("result = " + result + " for id: " + id);
return result;
}
}
测试
import org.easymock.EasyMockSupport;
import org.junit.jupiter.api.Test;
import static org.easymock.EasyMock.expect;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ConfigTest extends EasyMockSupport {
// have your testClass extend EasyMockSupport, and use that to create your mock
private ConfigService service;
@Test
public void testCallingService() {
Config config=new Config(12345l);
service = createMock(ConfigService.class);
// as said before, use the createMock method you inherit
config.setConfigService (service );
expect(service.getId(12345l)).andReturn("mocked");
expect(service.getId(17L)).andReturn("smaller");
replayAll(); // don't forget the replayAll();
String result = config.callingService();
assertEquals( "mocked", result);
Config config2 = new Config(17L);
config2.setConfigService(service);
String result2 = config2.callingService();
assertEquals( "smaller", result2);
verifyAll(); // to end your test which uses a mock
}
// Simple test without the mock to show the difference
@Test
public void testNonMocked() {
Config config = new Config(50L);
config.setConfigService(new ConfigService());
String result = config.callingService();
assertEquals("hardcoded", result);
}
}
我用于测试的依赖项
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.0-M1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.0-M1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.5.0-M1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>4.0.2</version>
<scope>test</scope>
</dependency>