Skip to content

Commit

Permalink
junit demo mock other interface
Browse files Browse the repository at this point in the history
  • Loading branch information
edf91 committed Jan 1, 2018
1 parent dbae0fc commit eb9ee6c
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions junit-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<excludes>
<exclude>org/wxd/junit/demo/facade/*.class</exclude>
<exclude>org/wxd/junit/demo/*.class</exclude>
<exclude>org/wxd/junit/demo/api/*.class</exclude>
</excludes>
</instrumentation>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.wxd.junit.demo.api;

import org.springframework.stereotype.Service;

@Service
public class OtherInterface {

// 假设通过http等发起的请求获取信息
public String getOf(String id){
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.wxd.junit.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wxd.junit.demo.api.OtherInterface;

@Service
public class CallOtherInterfaceService {

@Autowired
private OtherInterface otherInterface;


public String of(String id){
return otherInterface.getOf(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.wxd.junit.demo.other;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses(value = {OtherInterfaceTest.class})
public class OtherInterfaceAllTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.wxd.junit.demo.other;


import org.junit.Assert;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.wxd.junit.demo.api.OtherInterface;
import org.wxd.junit.demo.service.CallOtherInterfaceService;

import static org.mockito.Mockito.*;

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
@FixMethodOrder(value = MethodSorters.JVM)
public class OtherInterfaceTest {

@MockBean
private OtherInterface otherInterface;
@Autowired
private CallOtherInterfaceService service;

private String id = "id";

@Before
public void setUp(){
/**
* 如果otherInterface 本地开发未完成,或者网络不通等,则可以通过mock打桩
*/
when(otherInterface.getOf(id)).thenReturn(id);
}

// 测试otherInterface
@Test
public void assertOfId(){
String qId = otherInterface.getOf(id);
Assert.assertEquals(qId,id);
// 校验 getOf(id) 是否被调用1次
verify(otherInterface,times(1)).getOf(id);
}

// 测试通过service 调用 otherService
@Test
public void assertCallOfId(){
String qId = service.of(id);
Assert.assertEquals(qId,id);
verify(otherInterface,times(1)).getOf(id);
}

}

0 comments on commit eb9ee6c

Please sign in to comment.