JUnit 测试在 GitHub 个操作上失败,但在本地测试失败
JUnit tests fails on GitHub Actions but not locally
我正在尝试 运行 JUnit 测试 GitHub 操作,但其中一些失败了。本地所有测试都通过了。在我的电脑上,我使用 Ubuntu 20.04 和 OpenJDK 1.8 (275),在 CI 使用 OpenJDK 1.8(来自标准操作)。
java -version
PC 输出:
openjdk version "1.8.0_275"
OpenJDK Runtime Environment (build 1.8.0_275-8u275-b01-0ubuntu1~20.04-b01)
OpenJDK 64-Bit Server VM (build 25.275-b01, mixed mode)
java-version
来自 GitHub 操作的输出:
openjdk version "1.8.0_275"
OpenJDK Runtime Environment (Zulu 8.50.0.53-CA-linux64) (build 1.8.0_275-b01)
OpenJDK 64-Bit Server VM (Zulu 8.50.0.53-CA-linux64) (build 25.275-b01, mixed mode)
测试断言:
@Test
public void test() {
assertEquals(
17,
new CountingSheep(
new Boolean[]{
true, true, true, false,
true, true, true, true,
true, false, true, false,
true, false, false, true,
true, true, true, true,
false, false, true, true
}
).solution()
);
}
代码:
package com.smlnskgmail.jaman.codewarsjava.kyu8;
import java.util.Arrays;
public class CountingSheep {
private final Boolean[] input;
public CountingSheep(Boolean[] input) {
this.input = input;
}
public int solution() {
return (int) Arrays
.stream(input, 0, input.length)
.filter(b -> b != null && b)
.count();
}
}
CI 检查失败:
com.smlnskgmail.jaman.codewarsjava.kyu8.CountingSheepTest > test FAILED
java.lang.AssertionError: expected:<17> but was:<0>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:645)
at org.junit.Assert.assertEquals(Assert.java:631)
at com.smlnskgmail.jaman.codewarsjava.kyu8.CountingSheepTest.test(CountingSheepTest.java:11)
JUnit 实现:
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
GitHub 操作配置:
name: Build
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
env:
GRADLE_OPTS: -Dorg.gradle.daemon=false
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build with Gradle
run: ./gradlew build --info
但是这个测试在一个月前就通过了,我不能更改代码。
代码库:https://github.com/fartem/codewars-java
解决方案:here 是来自项目的 class。不执行这个 class 所有测试都通过了。
我认为这是 GitHub 上的一个错误。测试似乎通过了,我在 https://github.com/fartem/codewars-java/pull/1 创建了一个只包含该测试的分支,似乎成功通过了。
鉴于唯一改变的是 Java 的版本随您的结帐操作具体化,我想知道 GitHub 是否有一个临时损坏的 Java 版本被滚动返回。
顺便说一下,您在测试中使用了一个 Boolean
数组,但在这种情况下,您可以使用一个 boolean
数组来代替——这样,您的过滤器就不会不需要b != null
检查那里。
--
根据 fartem 的观察,还有一段代码 运行 并将 Boolean.TRUE
的值更改为 false
。变化是测试的 运行dom 排序。
Field field = Boolean.class.getField("TRUE");
field.setAccessible(true);
Field modifiedField = Field.class.getDeclaredField("modifiers");
modifiedField.setAccessible(true);
modifiedField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, false);
因此,在此测试 运行 之后对布尔值使用自动装箱的任何测试都会得到错误的结果。
我正在尝试 运行 JUnit 测试 GitHub 操作,但其中一些失败了。本地所有测试都通过了。在我的电脑上,我使用 Ubuntu 20.04 和 OpenJDK 1.8 (275),在 CI 使用 OpenJDK 1.8(来自标准操作)。
java -version
PC 输出:
openjdk version "1.8.0_275"
OpenJDK Runtime Environment (build 1.8.0_275-8u275-b01-0ubuntu1~20.04-b01)
OpenJDK 64-Bit Server VM (build 25.275-b01, mixed mode)
java-version
来自 GitHub 操作的输出:
openjdk version "1.8.0_275"
OpenJDK Runtime Environment (Zulu 8.50.0.53-CA-linux64) (build 1.8.0_275-b01)
OpenJDK 64-Bit Server VM (Zulu 8.50.0.53-CA-linux64) (build 25.275-b01, mixed mode)
测试断言:
@Test
public void test() {
assertEquals(
17,
new CountingSheep(
new Boolean[]{
true, true, true, false,
true, true, true, true,
true, false, true, false,
true, false, false, true,
true, true, true, true,
false, false, true, true
}
).solution()
);
}
代码:
package com.smlnskgmail.jaman.codewarsjava.kyu8;
import java.util.Arrays;
public class CountingSheep {
private final Boolean[] input;
public CountingSheep(Boolean[] input) {
this.input = input;
}
public int solution() {
return (int) Arrays
.stream(input, 0, input.length)
.filter(b -> b != null && b)
.count();
}
}
CI 检查失败:
com.smlnskgmail.jaman.codewarsjava.kyu8.CountingSheepTest > test FAILED
java.lang.AssertionError: expected:<17> but was:<0>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:645)
at org.junit.Assert.assertEquals(Assert.java:631)
at com.smlnskgmail.jaman.codewarsjava.kyu8.CountingSheepTest.test(CountingSheepTest.java:11)
JUnit 实现:
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
GitHub 操作配置:
name: Build
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
env:
GRADLE_OPTS: -Dorg.gradle.daemon=false
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build with Gradle
run: ./gradlew build --info
但是这个测试在一个月前就通过了,我不能更改代码。
代码库:https://github.com/fartem/codewars-java
解决方案:here 是来自项目的 class。不执行这个 class 所有测试都通过了。
我认为这是 GitHub 上的一个错误。测试似乎通过了,我在 https://github.com/fartem/codewars-java/pull/1 创建了一个只包含该测试的分支,似乎成功通过了。
鉴于唯一改变的是 Java 的版本随您的结帐操作具体化,我想知道 GitHub 是否有一个临时损坏的 Java 版本被滚动返回。
顺便说一下,您在测试中使用了一个 Boolean
数组,但在这种情况下,您可以使用一个 boolean
数组来代替——这样,您的过滤器就不会不需要b != null
检查那里。
--
根据 fartem 的观察,还有一段代码 运行 并将 Boolean.TRUE
的值更改为 false
。变化是测试的 运行dom 排序。
Field field = Boolean.class.getField("TRUE");
field.setAccessible(true);
Field modifiedField = Field.class.getDeclaredField("modifiers");
modifiedField.setAccessible(true);
modifiedField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, false);
因此,在此测试 运行 之后对布尔值使用自动装箱的任何测试都会得到错误的结果。