当前位置: 首页 > 知识库问答 >
问题:

Maven org.opentest4j.AssertionFailedError(即使值相同)

段干飞翮
2023-03-14

使用Maven引导2。

在我的一个项目中,我们有一个单元测试用例,它在CI/CD和我的一台笔记本电脑中运行良好。

而在我的笔记本电脑中设置相同的项目,却返回了奇怪的异常。

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;

@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(locations = "classpath:application.properties")
@ContextConfiguration(classes = BookAutoConfiguration.class)
public class BookServiceTest {

@Autowired
    private BookService bookService;

 @Test
    public void testCurrencyDefaultFormat() {
        Book de = bookService.byCountryCode("DE");
        Book gb = bookService.byCountryCode("GB");
        Book es = bookService.byCountryCode("ES");
        Book it = bookService.byCountryCode("IT");
        double amount = 123.456;

        assertThat(de.formatAmountDefault(amount)).isEqualTo("123,46 €");
        assertThat(es.formatAmountDefault(amount)).isEqualTo("123,46 €");
        assertThat(it.formatAmountDefault(amount)).isEqualTo("€ 123,46");
        assertThat(gb.formatAmountDefault(amount)).isEqualTo("£123.46");
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sample.global</groupId>
    <artifactId>book-java-sdk</artifactId>
    <version>2.1.0</version>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <spring-boot.version>2.2.1.RELEASE</spring-boot.version>
        <jacoco-maven-plugin.version>0.8.6</jacoco-maven-plugin.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>



    <build>
        <plugins>
              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>before-tests</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>after-tests</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Java版本:

openjdk版本“11.0.10”2021-01-19 LTS openjdk运行时环境Corretto-11.0.10.9.1(build 11.0.10+9-LTS)openjdk 64位服务器VM Corretto-11.0.10.9.1(build 11.0.10+9-LTS,混合模式)

Maven版本:

Apache Maven 3.6.3(cecedd343002696d0abb50b32b541b8a6ba2883f)Maven主页:/usr/local/cellar/Maven/3.6.3_1/libexec Java版本:11.0.10,供应商:Amazon.com Inc.,运行时:/users/sam/.sdkman/candidates/Java/11.0.10.9.1-AMZN默认区域设置:en_GB,平台编码:UTF-8操作系统名称:“Mac OS X”,版本:“10.15.5”,ARCH:“X86_64”,系列:“Mac”

它失败,出现以下错误,它只发生在我的本地系统中,

testCurrencyDefaultFormat(com.book.repository.BookServiceTest)  Time elapsed: 0.026 sec  <<< FAILURE!
org.opentest4j.AssertionFailedError: 
Expecting:
 <"123,46 €">
to be equal to:
 <"123,46 €">
but was not.
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at com.book.repository.BookServiceTest.testCurrencyDefaultFormat(BookServiceTest.java:91)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        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.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
        at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
        at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
        at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
        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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
        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 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        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)

我还尝试了assertjisequaltonormalizingwhitespace&new line,但没有成功。当我在另一台笔记本电脑中运行相同的测试时,它可以正常工作,而在我的CI/CD中,它可以正常工作。

当我在intelliJ中使用runAs选项运行测试时,它工作良好,但当我执行mvn clean package时,它报告错误。

有没有编码问题。在settings.xml中也是UTF-8

对于这一问题,任何帮助都将非常感谢。

共有1个答案

苏涛
2023-03-14

您的pom.xml中的Java版本必须设置为11,而不是1.8

1.8 ion>

11

 类似资料:
  • 我正在尝试为我的rails应用程序中的图像做一个布局,但我在对齐方面遇到了麻烦。 HTML CSS 我以为flexbox是最好的方法。我想要的图像在窗口调整大小,这就是为什么我有宽度:100%。 编辑: 实际上,看起来我的问题是Flex-Box的一个重复:将最后一行对齐到网格。我解决了插入空元素来填充行的问题,就像第一个答案所暗示的那样。

  • 问题内容: 我知道栈上有很多类似的问题。但是常见的答案似乎并不适合我。 我有这样的文件结构 我发现如果我在test.py中包含代码 要么 我将能够引用目录中的模块。但这没有用。 所以我尝试添加到路径,它正确添加了它: 但是即使是现在,当我尝试上述导入语句时,我仍然收到错误消息 要么 有什么明显的我想念的吗? 有没有办法检查我是否正确配置了文件,或查看我的程序包层次结构? 问题答案: 您需要更新,这

  • 日志cat显示以下错误。 06-07 03:12:49.829 29544-29544/com。实例阿默。奥兰加巴德带系统。组织。json。JSONException:组织中的total没有值。json。JSONObject。在org上获取(JSONObject.java:389)。json。JSONObject。com上的getString(JSONObject.java:550)。实例阿默。奥

  • 问题内容: 您可能知道,这两种类型都是32位。只能容纳整数,而还支持浮点数(如类型名称所示)。 那么怎么可能最大的值为2 31,最大的值为 3.4 * 10 38,而它们都是32位? 我认为的最大容量应大于浮点数,因为它不会为浮点数节省内存,并且仅接受整数。在这种情况下,我很高兴为您提供解释。 问题答案: 您的直觉很正确地告诉您,一个 信息内容 中的 信息内容 不能超过另一个,因为它们都有32位。

  • Hy, 我对JAVA有点陌生,有个大问题。当我向