Java 模仿Util类';使用gmock的s静态方法

Java 模仿Util类';使用gmock的s静态方法,java,unit-testing,groovy,gmock,Java,Unit Testing,Groovy,Gmock,我想对SomeService.getValue方法进行单元测试 我正在尝试以下方法: class SomeService{ public String getValue(){ return SomeUtil.generateValue(); } } class SomeUtil{ public static String generateValue() { return "yahoo"; } } 但是它失败了,因为util方法实际上并没有受到嘲笑 问

我想对
SomeService.getValue
方法进行单元测试

我正在尝试以下方法:

class SomeService{
  public String getValue(){
     return SomeUtil.generateValue();
  }
}

class SomeUtil{
   public static String generateValue() {
      return "yahoo";
   }
}
但是它失败了,因为util方法实际上并没有受到嘲笑

问题


如何对我的服务方法进行单元测试?

我做了一个快速测试,它可以轻松地工作:

@Test
void "getValue should return whatever util gives"(){
    def mockSomeUtil = mock(SomeUtil)
    mockSomeUtil.static.generateValue().returns("blah")
    play {
        Assert.assertEquals(someService.getValue(), "blah")
    }
}
如果需要多次调用该服务,则可能需要一个
存根
,即:

@Grapes([
    @Grab(group='org.gmock', module='gmock', version='0.8.3'),
    @Grab(group='junit', module='junit', version='4.12')
])

import org.gmock.*
import org.junit.*
import org.junit.runner.*

class SomeService {
    public String getValue(){
        return SomeUtil.generateValue()
    }
}

class SomeUtil {
    public static String generateValue() {
        return "yahoo"
    }
}

@WithGMock
class DemoTest {
    def someService = new SomeService()

    @Test
    void "getValue should return whatever util gives"() {
        def mockSomeUtil = mock(SomeUtil)
        mockSomeUtil.static.generateValue().returns("blah")

        play {
            Assert.assertEquals(someService.getValue(), "blah")
        }
    }
}

def result = JUnitCore.runClasses(DemoTest.class)
assert result.failures.size() == 0

我做了一个快速测试,它可以轻松工作:

@Test
void "getValue should return whatever util gives"(){
    def mockSomeUtil = mock(SomeUtil)
    mockSomeUtil.static.generateValue().returns("blah")
    play {
        Assert.assertEquals(someService.getValue(), "blah")
    }
}
如果需要多次调用该服务,则可能需要一个
存根
,即:

@Grapes([
    @Grab(group='org.gmock', module='gmock', version='0.8.3'),
    @Grab(group='junit', module='junit', version='4.12')
])

import org.gmock.*
import org.junit.*
import org.junit.runner.*

class SomeService {
    public String getValue(){
        return SomeUtil.generateValue()
    }
}

class SomeUtil {
    public static String generateValue() {
        return "yahoo"
    }
}

@WithGMock
class DemoTest {
    def someService = new SomeService()

    @Test
    void "getValue should return whatever util gives"() {
        def mockSomeUtil = mock(SomeUtil)
        mockSomeUtil.static.generateValue().returns("blah")

        play {
            Assert.assertEquals(someService.getValue(), "blah")
        }
    }
}

def result = JUnitCore.runClasses(DemoTest.class)
assert result.failures.size() == 0

你在扩展GMockTestCase吗?我在使用@WithGMock Annotation你在扩展GMockTestCase吗?我在使用@WithGMock Annotation我看到的唯一区别是我在用TestNG运行它,但我认为它不应该有任何区别OK…所以一旦我在单独的文件中提取服务和util类,这就开始失败。这让我回到了同一个问题我看到的唯一区别是我用TestNG运行它,但我认为它不应该有任何区别OK…所以一旦我在单独的文件中提取服务和util类,它就开始失败。这让我回到了同一个问题