包含junitmaven的词条
本篇文章给大家谈谈junitmaven,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
如何在maven中添加junit
安装 Maven 之前要求先确定你的 JDK 已经安装配置完成。Maven是 Apache 下的一贺亏个项目,目前最新版本是 3.0.4,我用的也是这个。 首先去官网下载 Maven 下载完成之后将其解压,我将解压后的文件夹重命游拍名成禅磨神 maven,并将它放在 D:\Server 目录下
[img]maven是怎么运行junit的
1.首先maven项冲答返目要添加JUnit对应的jar包。
打开项举宏目,双击pom.xml文件,添加如下代码后保存,然后maven install一下,引入JUnit对应的jar包。
dependency
groupIdjunit/groupId
artifactIdjunit/artifactId
version4.11/version
/dependency
2.选中你要测试的那个类,右击 new --散饥 other -- junit test case
3然后点击next ,选中你需要测试的方法,然后finish 就行了,单元测试类创建完成,开始编写测试代码。
maven项目中junit测试同一方法, 运行一次却重复执行了两次?
笔记
描述:在当前的junit class定义了一个方法,test,里面的逻辑是做一个数据库插入操作
结果:插入操作执行了2次
父类厅缺空
@RunWith(SpringJUnit4ClassRunner.class)
public abstract class AbstractTest {
@Before
public void test() {
MockitoAnnotations.initMocks(this);
}
}
执行逻辑的junit
@ContextConfiguration(locations = { "classpath*:demo.xml" })
public class DaoTest extends AbstractTest{
@Resource
Dao dao;
@Test
public void test() {
DaoModel model = new DaoModel();
model.setUpdateTime(new Date());
dao.saveObj(model);
}
}
debug的结果: 由于父类@Before方法和扮瞎子类的@Test方法重名造成的。
分析:spring构造了org.junit.internal.runners.statements.RunBefores
流程
SpringJUnit4ClassRunner.withBefores -
BlockJUnit4ClassRunner.withBefores -
new RunBefores
public RunBefores(Statement next, ListFrameworkMethod befores, Object target) {
this.next = next;
this.befores = befores;
this.target = target;
}
它的befores就是AbstractTest,next是当前的DaoTest,target是model累
然后继续执行,
SpringJUnit4ClassRunner.runChild -
org.junit.runners.model.FrameworkMethod的invokeExplosively方法
public Object invokeExplosively(final Object target, final Object... params)
throws Throwable {
return new ReflectiveCallable() {
@Override
protected Object runReflectiveCall() throws Throwable {
return method.invoke(target, params);
}
}.run();
}
这时候method是AbstractTest的test方法,target是DaoTest类,则扮绝这时执行了一次DaoTest的test方法。
test方法执行完后,在进入RunBefores的evaluate方法,这时候的next是DaoTest,则导致了又执行了一次test方法。
so,更改父类的@Before方法名称。
关于junitmaven和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。