【问题标题】:Reference to assertEquals is ambiguous when running a unit test运行单元测试时对 assertEquals 的引用不明确
【发布时间】:2017-07-13 14:19:56
【问题描述】:

在我的应用程序中

`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

我试图在我的springmvchibernate 项目中进行junit 测试。我试图在我的insert 模块中进行测试。但它给出了上述错误。 我看到了许多教程和参考资料,但我无法处理该错误。 提前致谢。

【问题讨论】:

  • 我想categoryDao.addCategoryBoolean 类型。我说的对吗?
  • 或...assertTrue("successfully inserted", categoryDao.addCategory(category))

标签: java spring spring-mvc junit


【解决方案1】:

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

这两种方法 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 情况时,才可以返回nullBoolean 允许)。但是加法运算要么是真要么是假。
null 可能意味着什么?
所以,我认为这应该返回boolean。 这样,您的代码就可以很好地编译,因为编译器绑定的方法没有任何歧义:

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

assertEquals() 还是 assertTrue()?

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

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

【讨论】:

  • 是的 assertTrue() 应该在这里做的事情。
  • '提供返回 null 的可能性(如布尔值允许的那样)' - 今天仍然有用!
【解决方案2】:

替换

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

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

【讨论】:

    【解决方案3】:

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

    即 你可能会调用它,

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

    或者,

    assertEquals("sucessfully inserted..", true, TRUE or FALSE);
    // with Boolean values
    

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

    public static void assertEquals(String message, 预期对象, 实物)

    public static void assertEquals(String message, 预期的布尔值, 布尔实际)

    因此,如果您的 第三个参数是原始值 布尔 值,则不应有关于调用哪个方法的歧义(它显然应该是第一个)。 但是如果它不是一个原始布尔值,那么对于您在这里指的是哪种方法存在歧义。

    参考:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 2012-11-28
      • 2018-01-21
      • 1970-01-01
      • 2013-04-07
      • 1970-01-01
      相关资源
      最近更新 更多