繁体   English   中英

使用Mockito注释与spring

[英]Using Mockito annotations with spring

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

可以说我有以下场景

这是我的webservice接口

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();
    }

}

当我运行测试时,我看到的是,我从网络服务中得到了我在提交时提到的内容。

谁能帮我这个?

我通过使用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();
    }

}

@Spy用于监视调用服务时发生的事情(对于例如断言是否存在传递性方法调用很有用),你想要的是@Mock注释:

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

这将导致所有myService方法返回null,禁止使用doReturn / when覆盖的方法。

顺便说一句,您应该使用@Before来注释它,而不是从测试方法中调用setUpTest()

干杯,

在某些测试中使用模拟替换你的Bean使用Springockito或更好的Springockito注释

这样的事情应该有效:

@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();
    }

}

你的意思是setUp方法中的anyString()而不是“someString”吗? 如果您使用特定字符串调用方法,这也可以是eq("someString")

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

@Mock MyService myService;


public void setUpTest() {
    MockitoAnnotations.initMocks(this);
    when(myService.doSomethingElse(anyString)).thenReturn(123);
}

改性,

    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();
    }
}

这应该适合您的需要:

@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 */);
    }
}

另见官方文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM