SpringJUnit4ClassRunner+mockito (二)

pom.xml

 1 <dependency>
 2     <groupId>org.springframework.boot</groupId>
 3     <artifactId>spring-boot-starter-test</artifactId>
 4     <scope>test</scope>
 5 </dependency>
 6 
 7 <dependency>
 8     <groupId>org.powermock</groupId>
 9     <artifactId>powermock-api-mockito2</artifactId>
10     <version>2.0.9</version>
11     <scope>test</scope>
12     </dependency>
13 </dependencies>

mock 私有属性

1 public class MtkShowServiceImpl  {
2 
3     private String prefixUrl;
4     
5     //以下代码省略....
6 }    

test类:

1   @Autowired
2   private MtkShowServiceImpl mtkShowService;
3 
4   @Before
5   public void before() {
6       String prefixUrl = "";
7       Whitebox.setInternalState(mtkShowService, "prefixUrl", prefixUrl);
8 
9   }

mock public static 属性

1 public class MtkConfig {
2 
3     public static String XP_URI_PREFIX;
4    // 以下代码省略.... 
5 } 

test类:

1 @Before
2 public void before() {
3    Whitebox.setInternalState(MtkConfig.class, "XP_URI_PREFIX", "http://192.168.200.188:8280/crystal-mtk");
4 }

 

posted @ 2023-02-27 11:27  爱,诗意永存  阅读(113)  评论(0编辑  收藏  举报