AssertEquals reference is ambiguous when running unit test

In my application

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

      

My code

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

}

      

Mistake:

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 mine springmvc

with hibernate

project.I am trying to test in my module insert

but it gives the above error. I've seen a lot of tutorials and links, but I can't seem to get my head around this error. Thanks in advance.

+3


source to share


3 answers


When the compiler tries to associate a method call with one particular method, if it fails to identify a method more specific than others, it emits a compilation error. This is your case.

both assertEquals (java.lang.String, boolean, boolean) methods in org.testng.AssertJUnit

and the assertEquals (java.lang.String, java.lang.Object, java.lang.Object) method in org.testng.AssertJUnit

match

If you have this compile-time ambiguity problem, it means that you are not calling a method assertEquals()

with two primitives boolean

as arguments.

So, categoryDao.addCategory(category)

returns are very likely boolean

, not boolean

.

Boolean or logical return?



Providing a return option null

(as it allows boolean

) only makes sense if you need to handle a case null

. But the add operation is either true or false.
What can a mean null

?
So, I think this should come back boolean

. This way the code will compile just fine as the compiler-related method will be without any ambiguity:

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

...

assertEquals () or assertTrue ()?

Alternatively, to assert that an expression is true, you can simply use Assert.assertTrue()

a more explicit method:

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

      

+3


source


Replace

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

      



from

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

      

0


source


I guess it depends on what it returns categoryDao.addCategory(category)

. Since you used it to test for equality with true

which is a boolean, it can return a primitive boolean

or an object wrapper boolean

.

i.e. you can refer to it like,

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

      

or,

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

      

check below two methods in org.testng.AssertJUnit

,

public static void assertEquals (String message, Expected Object, Actual Object)

and

public static void assertEquals (String message, boolean expected, boolean actual)

So, if your third parameter is a primitive boolean , there should be no ambiguity about which method to call (it should come first). But , if it is not a prmitive boolean, then there is uncertainty as to which method you mean here.

Contact:

0


source







All Articles