Eclipse使用 junit测试 报 “The builder launch configuration could not be found” 错误的解决办法

 eclipse, junit  Eclipse使用 junit测试 报 “The builder launch configuration could not be found” 错误的解决办法已关闭评论
4月 112022
 

Eclipse使用 junit测试, 报 “The builder launch configuration could not be found” 错误

解决方法如下:

  1. Package Explorer 视图下选中项目名
  2.  右键项目名,然后选择Properties 或者 在Eclipse 工具栏中选择 Project -> Properties
  3. 弹出的 Properties 对话框中,点击 Builders
  4. 将没有勾选,缺失的 builder 移除掉
  5. 点击 OK 按钮

DONE!

指定junit测试方法执行顺序

 junit  指定junit测试方法执行顺序已关闭评论
2月 202014
 

以前一直以为junit测试都是按照测试方法的顺序执行了,某次测试中发现原来并非如此。

在junit4.11后,可以通过对测试类添加注解 “@FixMethodOrder(value)” 来指定,其中value 为执行顺序,三种执行顺序可供选择:默认(MethodSorters.DEFAULT),按方法名(MethodSorters.NAME_ASCENDING)和JVM(MethodSorters.JVM),当没有指定任何顺序时,按默认来执行。


1. MethodSorters.DEFAULT

默认顺序由方法名hashcode值来决定,如果hash值大小一致,则按名字的字典顺序确定

由于hashcode的生成和操作系统相关(以native修饰),所以对于不同操作系统,可能会出现不一样的执行顺序,在某一操作系统上,多次执行的顺序不变

2. MethodSorters.NAME_ASCENDING (推荐
按方法名称的进行排序,由于是按字符的字典顺序,所以以这种方式指定执行顺序会始终保持一致;

不过这种方式需要对测试方法有一定的命名规则,如 测试方法均以testNNN开头(NNN表示测试方法序列号 001-999)

3. MethodSorters.JVM
按JVM返回的方法名的顺序执行,此种方式下测试方法的执行顺序是不可预测的,即每次运行的顺序可能都不一样(JDK7里尤其如此).

eclipse + Spring + junit的测试

 junit, spring  eclipse + Spring + junit的测试已关闭评论
3月 062013
 

转自:http://xuyufei1985.blog.163.com/blog/static/13164348200911212942824/

开发一个eclipse3.4+Struts2.0+hibernate3.2+Spring2.5+oracle10g的项目。实现完DAO层后,想对Dao层做下测试。以前都是用的Junit3.8,这次换了最新的Junit4.4,但是在写测试类时遇到不少麻烦,将碰到的问题和解决方法记录下来,供后来的朋友们参考。

下面分别讲解在Junit4.4和Junit3.8下如何写测试类。
对于Junit3.8:
Spring的DAO层的测试,spring-test.jar包里有个AbstractTransactionalDataSourceSpringContextTests类,只要继承它,然后重写getConfigLocations方法就可以了。比较简单,而且又支持回滚,没有数据库的耗时操作。
对于你所有的applicationContext.xml等spring配置文件,你都不用动。Eclipse默认的classpath路径是项目路径下的build/classes,注意不要被你的ant写的build覆盖了,而ant的build又没有编译test包。最终找不到测试类。
 
下面先写一个基本的测试类,其他的只要继承这个类并添加test方法即可。

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
 
public class BaseDaoTestCaseJunit3 extends
AbstractTransactionalDataSourceSpringContextTests {
 
protected final static Log log = LogFactory.getLog(BaseDaoTestCaseJunit3.class);
 
@Override
protected String[] getConfigLocations(){
this.setAutowireMode(AUTOWIRE_BY_NAME);
return new String[]{"classpath:/applicationContext.xml","classpath:/spring-config-*.xml"};
}
 
public void testConfig() {
       assertNotNull("spring-mock context has bean init()",this.applicationContext);
    }
}

下面这个类继承自上面的BaseDaoTestCaseJunit3,只是测试了下CustInfoDao里面的findByEmail方法。
import com.psi.domain.CustInfo;
 
public class CustInfoDaoTests3 extends BaseDaoTestCaseJunit3 {
private CustInfoDao ciDao;
private CustInfo ci;
 
public CustInfoDaoTests3(){}
 
public void setCustInfoDao(CustInfoDao ciDao){
this.ciDao = ciDao;
}
 
public void getList(){
try{
ci = ciDao.findByEmail("[email protected]"); //此方法直接返回对象.
assertNotNull(ci.getId());
}catch(Exception e){
e.printStackTrace();
}
}
}

 
下面讲讲Junit4.4的写法:
Junit4用了完全不同于Junit3的写法,它的配置文件等都只需要在annotation里标注即可。TransactionManager是你在application里配置的TransactionManager。

package com.psi.dao;
 
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/applicationContext.xml","classpath:/spring-config-*.xml"})
@TransactionConfiguration(transactionManager="transactionManager",defaultRollback=true)
@Transactional
public class BaseDAOTestCaseJunit44 {
//本类作为基类,所有要用到的东西都在annotation里配置了。所以什么内容都不用写了。
}
 

//Dao测试类继承自上面的基类,并添加了个测试方法findByEmail。

 
import static org.junit.Assert.*;
 
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
 
import com.psi.domain.CustInfo;
 
public class CustInfoDaoTests extends BaseDAOTestCaseJunit44 {
private CustInfoDao ciDao;
private CustInfo ci;
 
public CustInfoDaoTests(){}
 
@Autowired
public void setCustInfoDao(CustInfoDao ciDao){
this.ciDao = ciDao;
}
 
@Before
public void init(){}
 
@Test
public void getList(){
try{
ci = ciDao.findByEmail("[email protected]");
assertNotNull(ci.getId());
}catch(Exception e){
e.printStackTrace();
}
}
}


就这么简单,但是要注意要加载相应的包,Spring-test.jar, apache的commons-下的包最好都加上。(项目中你基本都能用上)

还要注意你的配置文件路径。
我还碰到过下面这个错误:
Caused by: java.lang.NullPointerException
at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:374)

这是Eclipse的Bug,它在测试环境下不能自动加载到SystemLibrary下的所有包,所以你需要将你的Library改成user-library即可。