Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference to assertEquals is ambiguous when running a unit test

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.

like image 521
Binod Pant Avatar asked Jul 13 '17 14:07

Binod Pant


People also ask

What is the method assertEquals () used for?

assertEquals. Asserts that two objects are equal. If they are not, an AssertionError is thrown with the given message. If expected and actual are null , they are considered equal.

What is assertEquals in JUnit?

There is a method called assertEquals in the JUnit library that can be used to check if two objects is equally defined or not. It can be used to check if a specific instance of an object is expected on a method called by the test, or if na object passed through a method was “polymorphed” correctly.

What can we use instead of assertEquals?

Use assertThat instead An alternative option to assertEquals is assertThat .


1 Answers

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));
like image 79
davidxxx Avatar answered Sep 28 '22 00:09

davidxxx