3

Below is the Simple Calculator Application and I want to test it using Junit4 and mockito Framework i test that application on Junit4 and it works properly but I am facing problem on mockito implementation

CalculatorOperationsInterfaces.java

public interface CalculatorOperationsInterfaces { public double add(double input1, double input2); public double subtract(double input1, double input2); public double multiply(double input1, double input2); public double divide(double input1, double input2); } 

MainActivity.java

import android.os.Bundle; import android.support.v7.app.AppCompatActivity; public class MainActivity extends AppCompatActivity implements CalculatorOperationsInterfaces { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public double add(double input1, double input2) { return (input1 + input2); } @Override public double subtract(double input1, double input2) { return (input1 - input2); } @Override public double multiply(double input1, double input2) { return (input1 * input2); } @Override public double divide(double input1, double input2) { if(input2==0) return -1; else return (input1 / input2); } } 

CalculatorOperationsMockTest.java

import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import static org.mockito.Mockito.when; // @RunWith attaches a runner with the test class to initialize the test data @RunWith(MockitoJUnitRunner.class) public class CalculatorOperationsMockTest { //@Mock annotation is used to create the mock object to be injected @Mock CalculatorOperationsInterfaces calcService; @Before public void setUp() { calcService = new MainActivity(); } @After public void tearDown() { calcService = null; } @Test public void add(){ //add the behavior of calc service to add two numbers when(calcService.add(10.0,20.0)).thenReturn(30.00); //test the add functionality Assert.assertEquals(calcService.add(10.0, 20.0),30.0,0); } } 

I successfully test the Above MainActivity.java using Junit but I am new to Mockito framework when i try to run the CalculatorOperationsMockTest.java File i am getting the below error and i do not know how to solve it

Class not found: "myappmania.local_jvm_unit_test.CalculatorOperationsMockTest"Empty test suite. 

1 Answer 1

3

There are at least 3 problems here:

  1. The problem with "Empty Test Suite" error message can be solved from looking at this answer - make sure you have a Run Configuration that specifies the InstrumentationRunner.
  2. You can't get an instance of the Activity by calling new MainActivity(). The correct idiom is:

    @Rule public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class, true, false); 

    and then:

    calcService = activityRule.launchActivity(null); 
  3. You are not accomplishing anything by mocking CalculatorService. When testing, you use mocks to test the behaviour of a program called the 'system under test'. Here, your system under test is your implementation of CalculatorService i.e., the Activity. You don't have any dependencies to inject, as far as I can see. I hope this tutorial on mocking can clarify these concepts for you.

Please also check out the instructions in the official tutorial for instrumented unit tests in Android.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.