Skip to content
Advertisement

Mockito testing method void

I have a method called calculate

public class Calculator {
  public void calc(int a, int b){ 
    String condition = "first";
   int result = 0;

   if (condition.equals("first") { 
     condition = "no need";
   else { result = a + b;}

after which the result is printed, whatever, i need to make a test on which i see if the value of condition was changed to “no need” or it remained “first”, how can i check this using mockito testing? I tried so far, but i did not succeed. A good theoretical answer would be good, no need for code.

Advertisement

Answer

A good theoretical answer would be good

Mockito isn’t a testing framework, it’s a mocking framework.

You don’t test things with Mockito, you just substitute behavior in dependencies, in order that you can test the behavior of your code under specific and otherwise hard-to-reproduce conditions.

So, the premise of the question is flawed, not simply because you’re not using Mockito correctly, but also because you have no dependencies in this code for which behavior might be substituted.


But assuming you actually mean “how can I test this with a testing framework”: you can’t do that either.

Testing frameworks are just Java programs that run your Java program. They’re not magic: all they can test for are return values, thrown exceptions, and side effects (e.g. log messages, things printed, changes of state).

The only things being changed here are local variables. You can’t test anything about these because they are not observable outside the method in which they are defined.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement