【发布时间】:2016-03-08 13:33:06
【问题描述】:
我的一个测试(在几周内运行良好)开始失败,出现 JUnit v3.x 错误代码:
[junit] Testcase: testState took 1.079 sec
[junit] FAILED
[junit] null
[junit] junit.framework.AssertionFailedError
[junit] at com.test.PanelTest.testState(PanelTest.java:331)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:312)
[junit] at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
[junit] at java.util.concurrent.FutureTask.run(FutureTask.java:138)
[junit] at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
[junit] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
[junit] at java.lang.Thread.run(Thread.java:662)
在任何地方都没有对junit.framework 的引用。我在所有测试中都使用import org.junit.*(不推荐使用的那些不在 Jenkins 上测试的除外)。
编辑:从已弃用的测试中删除所有 junit.framework 导入。
我怀疑 Jenkins 有时仍在使用 JUnit 3.x,而它应该只使用 JUnit 4.x。我到处搜索是否存在其他 JUnit 版本,只剩下 JUnit 4.11 的 jar 文件。
无论我对测试代码进行什么更改,此错误都不会消失。我还尝试“擦除”工作区并验证 ant 脚本没有对 Junit 3.x 的任何引用。
这是我的 ANT 插件配置:
这是我的单元测试的精简版:
package com.test;
import org.fest.swing.core.GenericTypeMatcher;
import org.fest.swing.edt.FailOnThreadViolationRepaintManager;
import org.fest.swing.edt.GuiActionRunner;
import org.fest.swing.edt.GuiQuery;
import org.fest.swing.edt.GuiTask;
import org.fest.swing.fixture.*;
import org.junit.*;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ SomeFinalClass.class })
@PowerMockIgnore("javax.swing.*")
public class PanelTest {
private FrameFixture frameFixture = null;
private MyPanel panel = null;
private MyModel model = null;
@BeforeClass
public static void runOnce() {
FailOnThreadViolationRepaintManager.install();
}
@Before
public void setUp() throws Exception {
<TestFrame extends JFrame>
TestFrame testFrame = GuiActionRunner.execute(new GuiQuery<TestFrame>() {
@Override
protected TestFrame executeInEDT() throws Throwable {
<lots of mocking and stubbing>
return new TestFrame(panel);
}
});
GuiActionRunner.execute(new GuiTask() {
@Override
protected void executeInEDT() throws Throwable {
SwingUtils.setLookAndFeel();
SwingUtilities.updateComponentTreeUI(panel);
}
});
frameFixture = new FrameFixture(testFrame);
frameFixture.show();
frameFixture.robot.waitForIdle();
}
@After
public void tearDown() {
frameFixture.cleanUp();
}
@Test
public void testState() {
AnotherPanel otherPanel = myModel.getOtherPanel();
Assert.assertFalse(otherPanel.isShowing());
<create fake data>
myModel.update(fakeData);
JButtonFixture button = <get button Fixture>
button.click();
Assert.assertTrue(otherPanel.isShowing());
button.click();
Assert.assertFalse(otherPanel.isShowing()); <-- here is where it fails
}
}
【问题讨论】:
-
如何在 Jenkins 上运行测试?通过 Maven?
-
没有 Maven,只调用 ANT。
-
您是否在 jenkin 中安装了 ANT 插件,如果是,您是否在配置中将 Build 设置为“Invoke ANT”
-
是的,是的(添加了屏幕截图)。
-
就个人而言,我更喜欢同时使用 JMockit (jmockit.org) 而不是 Mockito 和 Powermock。如果是我,我肯定会进行实验,用 JMockit 替换 Mockito 和 Powermock,看看我是否还有另一个理由更喜欢 JMockit。只是一个想法,因为我知道绝望的时代需要绝望的措施。另外,我是 AssertJ (joel-costigliola.github.io/assertj) 的忠实粉丝。