gpt4 book ai didi

java - 运行单元测试时对 assertEquals 的引用不明确

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:08:18 26 4
gpt4 key购买 nike

在我的申请中

`CategoryDao` is a `interface`, `Category` is a model `class` 

我的代码是

CategoryTestCase.java

package com.binod.onlineshopping.category.test;
import com.binod.onlineshopping.category.dao.CategoryDao;
import com.binod.onlineshopping.category.model.Category;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static org.testng.AssertJUnit.assertEquals;

/**
* Created by binod on 7/13/17.
*/
public class CategoryTestCase {

private static AnnotationConfigApplicationContext context;
private static CategoryDao categoryDao;
private Category category;

@BeforeClass
public static void init() {

context = new AnnotationConfigApplicationContext();
context.refresh();
categoryDao = (CategoryDao) context.getBean("categoryDao");
}

@Test
public void addCategory(){
category=new Category();
category.setCname("Television");
category.setCdescription("TV is the product");
category.setImageUrl("c_Tv.png");
assertEquals("sucessfully inserted..",true,categoryDao.addCategory(category));
}

}

错误是:

Error:(34, 6) java: reference to assertEquals is ambiguous
both method assertEquals(java.lang.String,boolean,boolean) in org.testng.AssertJUnit and method assertEquals(java.lang.String,java.lang.Object,java.lang.Object) in org.testng.AssertJUnit match

我正在尝试 junit在我的 springmvc 中测试与 hibernate项目。我正在尝试在我的 insert 中进行测试module.but 它给出了上述错误。我看了很多教程和引用资料,但我无法处理该错误。提前致谢。

最佳答案

当编译器尝试将方法调用绑定(bind)到一个不同的方法时,如果它无法识别比其他方法更具体的方法,则会发出编译错误。这是你的情况。

both method assertEquals(java.lang.String,boolean,boolean) inorg.testng.AssertJUnit

and methodassertEquals(java.lang.String,java.lang.Object,java.lang.Object) inorg.testng.AssertJUnit

match

如果您在编译时遇到这种二义性问题,则意味着您没有以两个原始 boolean 作为参数调用 assertEquals() 方法。

所以 categoryDao.addCategory(category) 很可能返回 Boolean 而不是 boolean

boolean 值还是 boolean 值返回?

提供返回 null 的可能性(因为 Boolean 允许)只有当您需要处理 null 情况时才有意义。但是加法运算不是真就是假。
null 可能意味着什么?
所以,我认为这应该返回 boolean。通过这种方式,您的代码可以很好地编译,因为编译器绑定(bind)的方法没有任何歧义:

assertEquals(java.lang.String,boolean,boolean)

assertEquals() 还是 assertTrue()?

此外,要断言表达式是否为真,您可以简单地使用更明确的 Assert.assertTrue() 方法:

assertTrue("sucessfully inserted..", categoryDao.addCategory(category));

关于java - 运行单元测试时对 assertEquals 的引用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45083414/

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