-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
junit-demo/src/main/java/org/wxd/junit/demo/api/OtherInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
junit-demo/src/main/java/org/wxd/junit/demo/service/CallOtherInterfaceService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
junit-demo/src/test/java/org/wxd/junit/demo/other/OtherInterfaceAllTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
56 changes: 56 additions & 0 deletions
56
junit-demo/src/test/java/org/wxd/junit/demo/other/OtherInterfaceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |