Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在spring中使用Mockito注释_Java_Spring_Mockito - Fatal编程技术网

Java 在spring中使用Mockito注释

Java 在spring中使用Mockito注释,java,spring,mockito,Java,Spring,Mockito,我在应用程序中使用spring,我想为我所有的类编写单元测试。我从我的应用程序中调用了一些外部Web服务,我想用Mockito模拟它们,因为我只想测试我的功能 假设我有以下场景 这是我的Web服务接口 public interface MyWebService { public String getSomeData(int id); } 我是这样使用上述服务的 public interface MyService { int doSomethingElse(String str

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

假设我有以下场景

这是我的Web服务接口

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测试用例,如下所示。我在这里监视我的服务以便运行单元测试

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服务获得的值,而不是我在存根时提到的值


有人能帮我吗?

一个
@Spy
用于监视调用服务时发生的情况(并有助于断言是否存在可传递的方法调用),您需要的是
@Mock
注释:

public class class1Test {
    @Mock MyService myService;
    ...
这将导致所有
myService
方法返回null,禁止在
时使用
doReturn
/
重写这些方法

顺便说一句,与其从测试方法中调用
setUpTest()
,不如在
之前用
@对其进行注释


干杯,

这应该适合您的需要:

@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 */);
    }
}
另请参见。

设置中,您是指anyString()而不是“someString”
方法吗?如果使用特定字符串调用方法,也可以是
eq(“someString”)

根据我对Mockito的理解,我不使用间谍,因为它们可能表示类设计问题。你为什么不
@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();
    }
}

在测试中使用mock替换Bean,或者更好

像这样的方法应该会奏效:

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

}

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

    @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是因为,MyService中还有其他方法不需要存根。(这只是一个类似于我的示例代码)@coder我有一个类似的类设计,直到我来测试它时,我意识到我的类做了太多的事情,使单元测试变得困难。考虑将Web服务拆分为处理程序类。通过这种方式,您可以使用模拟处理程序构造类,并删除
@Spy
。任何对ws的调用都可以简单地用传统的非间谍工具来模拟。这可以解决问题,但遗憾的是我无法更改MyService或MyServiceImpl的原始实现。啊,是的,你使用间谍是正确的。我没有意识到。它们在处理第三方/遗留代码时特别有用。我关于匹配器的建议会产生更好的模拟结果吗?不会。我在这里面临的唯一问题是在同一个变量上使用“@Resource”和“@InjectMocks”。变量首先用“@resource”初始化,然后注入dosent。我没有一个构造函数将对象作为参数。我已经看到了上面的链接。如何使用springockito将“myService”间谍注入到“class1”中。在Class1中,“MyService”是私有的,没有setter。@coder,已更新。如果在您的真实环境中,myService被注入到Class1,它也将被注入WrapWithSpy。我正在使用spring java config初始化bean,springockito不支持它。@coder:似乎与SpringockitoAnnotatedContextLoader一起工作。class谢谢!这解决了我的问题。但是我使用“ReplaceWithMock”和“Autowired”注释组合来监视真正的Springbean