Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 运行单元测试时,对assertEquals的引用不明确_Java_Spring_Spring Mvc_Junit - Fatal编程技术网

Java 运行单元测试时,对assertEquals的引用不明确

Java 运行单元测试时,对assertEquals的引用不明确,java,spring,spring-mvc,junit,Java,Spring,Spring Mvc,Junit,在我的申请中 `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.mod

在我的申请中

`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
我正试图用
hibernate
项目在我的
springmvc
中进行
junit
测试。我正试图在我的
insert
模块中进行测试。但它给出了上述错误。 我看过很多教程和参考资料,但我无法处理这个错误。 提前感谢。

更换

assertEquals("sucessfully inserted..",true,categoryDao.addCategory(category));


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

中的两个方法assertEquals(java.lang.String、boolean、boolean) org.testng.AssertJUnit

方法 中的assertEquals(java.lang.String、java.lang.Object、java.lang.Object) org.testng.AssertJUnit

匹配

如果您在编译时遇到这种歧义问题,这意味着您不会使用两个原始的
boolean
作为参数来调用
assertEquals()
方法。

因此
categoryDao.addCategory(category)
很可能返回
Boolean
而不是
Boolean

布尔或布尔返回?

只有当您需要处理
null
案例时,才有可能返回
null
(因为
Boolean
允许)。但是加法运算不是真就是假。
一个
null
可能意味着什么?
因此,我认为这应该返回
布尔值。
这样,您的代码可以很好地编译,因为编译器绑定的方法不会有任何歧义:

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

assertEquals()或assertTrue()?

此外,若要断言表达式是否为真,只需使用更显式的
assert.assertTrue()
方法:

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

我认为这取决于
categoryDao.addCategory(category)
返回的内容。由于您使用它来检查与
true
的相等性,这是一个布尔值,因此它可能会返回一个基元
boolean
或一个对象包装器
boolean

i、 e。 您可能会调用它作为

assertEquals("sucessfully inserted..", true, true or false);
// with Primitive boolean values
或者

org.testng.AssertJUnit
中检查以下两种方法

公共静态void assertEquals(字符串消息, 预期的对象, 对象(实际)

公共静态void assertEquals(字符串消息, 布尔期望值, 布尔值(实际值)

因此,如果您的第三个参数是一个基本的布尔值,那么调用哪个方法不应该有歧义(它显然应该是第一个)。 但是如果它不是一个指定的布尔值,那么这里所指的方法就不明确了

参考:


我想
categoryDao.addCategory
Boolean
类型。我说的对吗?还是
assertTrue(“成功插入”,categoryDao.addCategory(category))
yeah assertTrue()应该在这里做这件事。“允许返回null(因为布尔值允许)”-今天仍然有用!
assertEquals("sucessfully inserted..", true, true or false);
// with Primitive boolean values
assertEquals("sucessfully inserted..", true, TRUE or FALSE);
// with Boolean values