The reference to assertEquals is ambiguous when performing a unit test

advertisements

In my application

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

My code is

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));
    }

}

The error is :

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

I am trying to junit test in my springmvc with hibernate project.I am trying to test in my insert module.but it gives a above error. I saw many tutorial and references but i am unable to handle that error. Thanks in advance.


When the compiler tries to bound a method invocation to one distinct method, if it doesn't manage to identify a method more specific than others, it emits a compilation error. It is your case.

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

If you have this ambiguity problem at compile time, it means that you don't invoke the assertEquals() method with two primitive boolean as arguments.

So categoryDao.addCategory(category) returns very probably Boolean and not boolean.

Boolean or boolean return ?

Giving the possibility to return null (as Boolean allows it) makes sense only if you need to handle the null case. But an adding operation is either true or false.
What a null could mean ?
So, I think that this should return boolean. In this way, you code would compile fine as the method bound by the compiler would be without any ambiguity :

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

assertEquals() or assertTrue()?

Besides, to assert if an expression is true, you may simply use Assert.assertTrue() method that is more explicit :

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