Junit执行及加载顺序分析(原创)

39 篇文章 0 订阅

在Eclipse中,我们可以采用Junit插件进行调试,那么Junit的执行及加载顺序是什么呢?下面我们通过实际的代码来分析一下。

首先展示的是一个类,功能是判断字符串是否为空,代码如下

package com.yourdomian.test;

public class TestClass{

 public void myJunit(){
  System.out.println("in TestClass's myJunit");
  
 }

}

下面是Junit的测试类

package com.yourdomian.test;

import junit.framework.Assert;
import junit.framework.TestCase;

public class TestClassTest extends TestCase {

 /*
  * @see TestCase#setUp()
  */
 protected void setUp() throws Exception {
  super.setUp();
  //myStringUtility = new StringUtility();
  System.out.println("in Junit's setUp");
 }

 /*
  * @see TestCase#tearDown()
  */
 protected void tearDown() throws Exception {
  super.tearDown();
  System.out.println("in Junit's tearDown");
 }

 public void testMyJunit() {
System.out.println("in TestClassTest 's testMyJunit");
 }

}

运行TestClassTest ,控制台结果为

in Junit's setUp
in StringUtilityTest's testMyJunit
in Junit's tearDown

如果在TestClassTest 中在加入一段代码,请看下面

package com.yourdomian.test;

import junit.framework.Assert;
import junit.framework.TestCase;

public class TestClassTest extends TestCase {

 /*
  * @see TestCase#setUp()
  */
 protected void setUp() throws Exception {
  super.setUp();
  //myStringUtility = new StringUtility();
  System.out.println("in Junit's setUp");
 }

 /*
  * @see TestCase#tearDown()
  */
 protected void tearDown() throws Exception {
  super.tearDown();
  System.out.println("in Junit's tearDown");
 }

 public void testMyJunit() {
System.out.println("in TestClassTest 's testMyJunit");
 }

 public void testOther() {
//这一段为新加的代码
 }

}

运行,控制台的结果为

in Junit's setUp
in StringUtilityTest's testMyJunit
in Junit's tearDown
in Junit's setUp
in Junit's tearDown

其实Junit没什么神秘的,现在应该对这个过程一目了然了吧~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值