gpt4 book ai didi

java - Sonarqube 不接受我的测试,但它在我的 Eclipse 中工作

转载 作者:太空宇宙 更新时间:2023-11-04 09:11:25 26 4
gpt4 key购买 nike

我有以下代码。它创建一个 10 秒的数字时钟。当我在 git 中进行提交时,SonarQube 说我所有的测试都失败了。但它可以在我的电脑上运行:\

public final class Clock implements Runnable {

private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(Clock.class);
private static JFrame frame = new JFrame();
private final Font clockFont;
private volatile boolean flag = true;


/**
* Clock constructor.
*/

public Clock() {

Clock.frame.setSize(1000, 250);
Clock.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Clock.frame.setVisible(true);
Clock.frame.setResizable(false);

this.clockFont = new Font("Serif", Font.BOLD, 40);

final Container contentArea = Clock.frame.getContentPane();
final ClockPanel timeDisplay = new ClockPanel();

contentArea.add(timeDisplay);
Clock.frame.setContentPane(contentArea);

}

主要方法


public static void main(final String[] args) {
final Clock clock = new Clock();
final Thread thread = new Thread(clock);
thread.start();
try {
Thread.sleep(10000);
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
Clock.LOGGER.info("Interrupted Exception was thrown", e);
}

clock.stopRunning();
Clock.LOGGER.info("program was successfully executed");
}

时间华盛顿方法

    public static String timeWashington() {

final Calendar now = Calendar.getInstance();
now.setTimeZone(TimeZone.getTimeZone("GMT-8"));

final String month = new SimpleDateFormat("M", Locale.ENGLISH).format(now.getTime());
final int date = now.get(Calendar.DATE);
final int hrs = now.get(Calendar.HOUR_OF_DAY);
final int min = now.get(Calendar.MINUTE);
final int sec = now.get(Calendar.SECOND);

return date + ", " + month + ", " + hrs + ":" + min + ":" + sec + " - Washington";
}

以及代码的延续

    @Override
public void run() {

while (this.flag) {
Clock.frame.repaint();

try {
Thread.sleep(1000);

} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
Clock.LOGGER.info("Interrupted Exception was thrown", e);
}

}
Clock.frame.setVisible(false);
Clock.frame.dispose();

}

/**
* stopRunning method.
*/
public void stopRunning() {
this.flag = false;
}

/**
* The ClockPanel fills current panel
*/
class ClockPanel extends JPanel {

private static final long serialVersionUID = 1L;

@Override
public void paintComponent(final Graphics painter) {

painter.setFont(Clock.this.clockFont);
painter.setColor(Color.black);
painter.drawString(Clock.timeWashington(), 40, 40);
}
}

}

我的 Junit 测试如下:

public class ClockTest {

@Rule
public final SystemOutRule log = new SystemOutRule().enableLog();

@Test
public void testMain() throws InterruptedException {
final String[] args = null;
Clock.main(args);
Assert.assertEquals("it should be equal", "program was successfully executed",
this.log.getLog());
}

@Test
public void testTimeWashington() {

boolean result = false;
final String time = Clock.timeWashington();
if ((time != null) && !time.isEmpty()) {
result = true;
}

Assert.assertTrue("This will succeed.", result);
}
}

请告诉我如何修复它。

失败ClockTest.testRunClock

最后 1 次构建失败(不稳定 # 288)花了 2 毫秒。

错误信息无法通过.iba.gomel.Clock 初始化类调用堆栈:

java.lang.NoClassDefFoundError: Could not initialize class by.iba.gomel.Clock
at by.iba.gomel.ClockTest.testRunClock(ClockTest.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.contrib.java.lang.system.internal.LogPrintStream$1$1.evaluate(LogPrintStream.java:30)
at org.junit.contrib.java.lang.system.internal.PrintStreamHandler$3.evaluate(PrintStreamHandler.java:48)
at org.junit.contrib.java.lang.system.internal.LogPrintStream$1.evaluate(LogPrintStream.java:26)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

最佳答案

根本问题似乎是您的测试正在寻找“Clock”类:

java.lang.NoClassDefFoundError: Could not initialize class by.iba.gomel.Clock

但是你的实现是Clock1

这对我来说意味着您运行测试的位置与本地计算机相比已经过时了。

(即从 Clock 重命名为 Clock1 或测试用例的相关更新尚未到达运行测试的位置)

关于java - Sonarqube 不接受我的测试,但它在我的 Eclipse 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59630269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com