java - Jenkins 使用错误的 JUnit 版本

标签 java jenkins junit

我的一个测试(几周后运行良好)开始失败,并出现 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 插件配置:

enter image description here

这是我的单元测试的精简版本:

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
    }
}

最佳答案

AssertionFailedError 仍然存在于 JUnit 4 上,并由已弃用的类 junit.framework.Assert 使用。

不要在 PanelTest 类上使用 junit.framework.Assert,而是使用 org.junit.Assert。验证导入。

关于java - Jenkins 使用错误的 JUnit 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35868838/

相关文章:

git - "FATAL: Error computing merge base"是什么意思?

java - 实例 void 方法的模拟无需调用 'expectLastCall' 方法即可工作

Java,使用反射进行列表排序

Sublime Text 2 中的 Java 控制台输入?

java - 从数据库设置图像

java - JUnit 测试 - 对模拟对象进行 Assets 或验证(或两者)?

java - JUnit 测试 Java

Java压缩集合库

jenkins - 在 Jenkins 管道中的 xfvb 包装内设置屏幕分辨率

powershell - 如何通过Powershell将Jenkins BUILD_NUMBER打印到.txt文件