spring web 业务系统单测使用Jmockit 进行夸层mock 也可以static 方法mock

来源:互联网 发布:英语在线造句软件 编辑:程序博客网 时间:2024/05/16 02:22

<dependency>
<groupId>com.googlecode.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>1.5</version>
<scope>test</scope>
</dependency>

现在jmockit已经转入

<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>1.19</version>

</dependency>



spring业务系统一般使用单例. 多层调用. 多层 mock

例如 A调用B,B调用C.

要测试A的方法,需要夸多层mock C的方法.

使用jmockit的NonStrictExpectations


@Servicepublic class A {    @Autowired    B b;    public void method() {        b.method();    }}





@Servicepublic class B {    @Autowired    IC c;    public void method() {        System.out.println("b=" + c.method());    }}


@Servicepublic class C implements IC {    @Override    public Integer method() {        System.out.println("123");        return 1;    }}



public interface IC {    public Integer method();}



@RunWith(SpringJUnit4ClassRunner.class)@Transactional@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,                         TransactionalTestExecutionListener.class })public class JmockitTestMock  {    @Autowired    A  a;    // 对接口进行mock,对应dubbo    @Autowired    IC ic;    // 夸层 mock.直接把该ic的object引用替换了    @Test    public void testMockit() {        new  NonStrictExpectations(ic) {            {             ic.method();             //可以对输入进行解析,不同的输入不同的返回.             result = Integer.valueOf(5); }            };         a.method();     }}
 

也可以对静态方法进行mock

 

new NonStrictExpectations(DynamicSettingsUtil.class) {      {        DynamicSettingsUtil.getString(anyString, anyString);        result = "123";      }  };




ps:

如果(ic)没有传入

 new  NonStrictExpectations(\) {            {                               ic.method();                result = Integer.valueOf(5);            }        };



 

会报如下错误:

    java.lang.IllegalStateException: Missing invocation to mocked type at this point; please make sure such invocations appear only after the declaration of a suitable mock field or parameter


方法二:

new MockUp<C>() {            @Mock            public Integer method() {               //可以对输入进行解析,不同的输入不同的返回.                System.out.println("MockUp<C> return 6");                return 6;            }};


ps: 没有没有@mock, 不会被mock.

  方法二,比较智能,即使你获取不到实例. 直接把整个jvm内的class替换掉. 一了百了.


注: 如果遇到执行挺不下来的情况, 死循环的征兆 . 看看 jmockit的classPath顺序是不是比junit后面,如果后面.前把classPath顺序放到前面去.

 

 

0 0
原创粉丝点击