Question

I am new to Mockito and need to learn it for work.

I made a very simple class that has one method that returns a string.

I then made the following test class in eclipse.

import static org.junit.Assert.*;

import org.junit.Test;
import org.mockito.Mockito;


public class No_1Test {

    @Test
    public void testNo_1() {
        No_1 myTest = Mockito.mock(No_1.class);
        Mockito.when(myTest.HelloWorld()).thenReturn("Hello World");

        String result = myTest.HelloWorld();
        Mockito.assertEquals("Hello World", myTest.HelloWorld());

    }

}

My understanding of what I have made so far is:

  1. I made a mock class of my No_1 class.
  2. I specified that whenever the HelloWorld() method is called it should return the string ("Hello World")
  3. I stored the results of HelloWorld() into the variable result (which should be "Hello World")
  4. I want to assert that it does what it was meant to do.

The problem is that in eclipse it says that the assertEquals method is undefined for Mockito.

Can someone please point out where I am going wrong here.

Was it helpful?

Solution

You are getting the error like assertEquals method is undefined for mockito because we can't use mockito as in mockito.assertEquals as in your codes try changing it with junit.assertEquals()

And What my experience on mockito says that you should avoid mock classes of the same project,we use to mock classes for which we are dependent on other projects or module,so don't mock No_1 class in your codes and try these codes::

import static org.junit.Assert.*;

import org.junit.Test;
import org.mockito.Mockito;

public class No_1Test {

   @Test
   public void testNo_1() {

      Mockito.when(myTest.HelloWorld()).thenReturn("Hello World");
      Junit.assertEquals("Hello World", myTest.HelloWorld());

   }
}

And mockito is for mocking java classes or method results but try using junit for your testing as in junit.assertequals

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top