8

我在我的应用程序中使用 spring,我想为我的所有类编写单元测试。我从我的应用程序中调用了几个外部 Web 服务,并且我想使用 Mockito 来模拟它们,因为我只想测试我的功能。

假设我有以下情况

这是我的网络服务接口

public interface MyWebService {
    public String getSomeData(int id);
}

我以这种方式使用上述服务

public interface MyService {
    int doSomethingElse(String str);
}

public class MyServiceImpl implements MyService {

    private MyWebService myWebService;

    int doSomethingElse(String str) {
        .....
        myWebService.getSomeData(id);
        ...
    }
}

public interface MyService1 {
    Stirng methodToBeTested();
}


public class Class1 implements MyService1{
    @Resource
    private MyService myService;

    public Stirng methodToBeTested() {
        myService.doSomethingElse("..");
    }
}

我写了如下的uint测试用例。我在这里监视 MyService 以便运行单元测试。

public class class1Test {

    @Spy
    MyService myService;

    @Resource
    @InjectMocks
    Class1 class1;

    public void setUPTest() {
        MockitoAnnotations.initMocks(this);
    Mockito.doReturn(123).when(myService).doSomethingElse("someString");
    }

    @Test
    public void methodToBeTestedTest() {
        this.setUPTest();
            ...
            class1.methodToBeTested();
    }

}

当我运行测试时,我看到的是,我从 web 服务中获得了我在存根时提到的值。

谁能帮我这个?

4

6 回答 6

6

我通过使用spring java config解决了这个问题。我在我的测试配置文件中扩展了我的默认配置文件。

    @Configuration
    public class TestConfig extends DefaultConfig {

      @Bean
      public MyService myService() {
        return Mockito.spy(new MyServiceImpl());
      }
    }

现在我的测试课是这样的

public class class1Test {

    @Resource
    MyService myService;

    @Resource
    Class1 class1;

    public void setUPTest() {
        MockitoAnnotations.initMocks(this);
    Mockito.doReturn(123).when(myService).doSomethingElse("someString");
    }

    @Test
    public void methodToBeTestedTest() {
        this.setUPTest();
            ...
            class1.methodToBeTested();
    }

}
于 2013-03-29T07:55:38.027 回答
3

A@Spy用于监视调用服务时发生的情况(并且对例如断言传递方法调用的存在或不存在很有用),您想要的是@Mock注释:

public class class1Test {
    @Mock MyService myService;
    ...

这将导致所有方法返回 null,除非您使用/myService覆盖那些方法。doReturnwhen

顺便说一句,setUpTest()您应该使用@Before.

干杯,

于 2013-03-26T11:03:34.250 回答
3

要在某些测试中用模拟替换您的 Bean,请使用Springockito或更好的 Springockito-annotations

像这样的东西应该工作:

@ContextConfiguration(loader = SpringockitoContextLoader.class,
    locations = "classpath:/context.xml")
public class SpringockitoAnnotationsMocksIntegrationTest extends AbstractJUnit4SpringContextTests {

    @WrapWithSpy
    private MyService innerBean;

    @Resource  
    Class1 class1;

    public void setUPTest() {        
        Mockito.doReturn(123).when(myService).doSomethingElse("someString");
    }

    @Test
    public void methodToBeTestedTest() {
    this.setUPTest();
        ...
        class1.methodToBeTested();
    }

}

于 2013-03-26T11:46:28.693 回答
2

您是指方法中的anyString()而不是“someString”setUp吗?eq("someString")如果您使用特定字符串调用方法,也可能出现这种情况。

根据我对 Mockito 的理解,我不使用间谍,因为它们可能表明存在类设计问题。你为什么不@Mock整个MyService,所以;

@Mock MyService myService;


public void setUpTest() {
    MockitoAnnotations.initMocks(this);
    when(myService.doSomethingElse(anyString)).thenReturn(123);
}
于 2013-03-26T11:10:29.457 回答
2

修改的,

    public class class1Test {

    @Mock
    private MyService myService;

    private Class1 class1;

    @Before
    public void onceBeforeEachTest() {
        MockitoAnnotations.initMocks(this);
         this.class1 = new Class1(myService); 
        Mockito.when(myService).doSomethingElse(anyString()).thenReturn(123);
    }

    @Test
    public void methodToBeTestedTest() {
            class1.methodToBeTested();
    }
}
于 2013-03-26T11:13:23.817 回答
1

这应该适合您的需要:

@RunWith(MockitoJunitRunner.class)
public class class1Test {

    @Mock
    private MyService myService;

    private Class1 class1;

    @Before
    public void setUp() {
        this.class1 = new Class1(myService); // creates this constructor.
    }

    @Test
    public void methodToBeTestedTest() {
        this.class1.methodToBeTested();
        Mockito.verify(this.myService).doSomethingElse(/* put expected arg here */);
    }
}

另见官方文档

于 2013-03-26T11:09:14.190 回答