JUnit - test that a method has been called - java

I'm new to unit testing, and I'm trying to test that a method has been called. The method in question doesn't return anything.
public void example (boolean foo) {
if (foo) {
processFoo(foo);
}
else if (foo==false) {
processSomethingElse(foo);
}
}
I want to be able to test that the processFoo method is being called, but I don't know how to do that.
If mocking is required, then I have to use JMockit. Thanks!

Sorry I'm a little late to the party, but I have a couple of ideas for you.
First, you mention that one option is to use JMockit--that's great as it gives you a lot of flexibility. If you use JMockit, then the visibility of your processFoo() method doesn't much matter. Let's see what that might look like:
public class Subject {
public void example (boolean foo) {
if (foo) {
processFoo(foo);
}
else if (foo==false) {
processSomethingElse(foo);
}
}
private void processFoo(boolean b) {
System.out.println("b = " + b);
}
private void processSomethingElse(boolean bb) {
System.out.println("bb = " + bb);
}
}
So, one caveat with this option, though is that I'm going to assume processFoo() is a method on your test subject and I'm going to use a partial mock to change the test subject--not something I really like to do, but this is an example. In general, it is best to only mock the dependencies of your test subject rather than behavior of the test subject itself--you have been advised! Note that the processFoo() method of the test subject is private. I'm going to substitute a method for the test with JMockit's partial mocking and the visibility of that new method does not have to match the original.
import static org.assertj.core.api.Assertions.assertThat;
import mockit.Mock;
import mockit.MockUp;
import mockit.integration.junit4.JMockit;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
#RunWith(JMockit.class)
public class SubjectTest {
private Subject testSubject = new Subject();
private boolean processFooCalled = false;
#Before
public void setup() {
new MockUp<Subject>() {
#Mock
public void processFoo(boolean b) {
processFooCalled = true;
};
};
}
#Test
public void should_call_processFoo() {
testSubject.example(true);
assertThat(processFooCalled).isTrue();
}
#Test
public void should_not_call_processFoo() {
testSubject.example(false);
assertThat(processFooCalled).isFalse();
}
}
Ok, so that was the first option. It's actually a little easier if you forget JMockit for this one, assuming you are able to subclass your test subject and override the processFoo() method:
public class Subject {
public void example (boolean foo) {
if (foo) {
processFoo(foo);
}
else if (foo==false) {
processSomethingElse(foo);
}
}
protected void processFoo(boolean b) { // NOTE: protected access here!
System.out.println("b = " + b);
}
private void processSomethingElse(boolean bb) {
System.out.println("bb = " + bb);
}
}
So, in this case, the strategy is simply to subclass your test subject and replace the implementation of the method you wish to observe being called. It might look like this:
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class SubjectTest2 {
private Subject testSubject = new TestableSubject();
private boolean processFooCalled = false;
#Test
public void should_call_processFoo() {
testSubject.example(true);
assertThat(processFooCalled).isTrue();
}
#Test
public void should_not_call_processFoo() {
testSubject.example(false);
assertThat(processFooCalled).isFalse();
}
class TestableSubject extends Subject {
#Override
protected void processFoo(boolean b) {
processFooCalled = true;
}
}
}
Give it a whirl. Hope it helps!

You could use a counter variable in your class and increment it whenever the method is called, or use a print statement. If you don't have access to the processFoo method, a simple approach would be to do this at the time that processFoo is called in another method, if that's the only place where it can possibly be called.
For example:
public static int processFooCalls = 0;
// ...
public void example (boolean foo) {
if (foo) {
processFoo(foo);
processFooCalls += 1;
// and/or
System.out.println("processFoo method was called");
}
// ...
}
public static void main (String[] args) {
// main routine here...
System.out.println("'processFoo' was called " + processFooCalls + " times.");
}
If processFoo can be called elsewhere, and you need to consider this possibility as well, then you'll need to have access to the processFoo code in order to do this, e.g.:
void processFoo( boolean b ) {
// increment number of times processFoo was called here, and/or print, as follows
processFooCalls += 1;
System.out.println("called processFoo method!");
/* some functionality */
}

Looking at the JMockit documentation, you will need the following tools:
Static Mocking: http://jmockit.github.io/tutorial/BehaviorBasedTesting.html#staticPartial
Invocation Counts: http://jmockit.github.io/tutorial/BehaviorBasedTesting.html#constraints
Combining the two in a test (my syntax may be a little off since I'm more accustomed to Mockito, but the concept should hold):
#Test
public void someTestMethod(#Mocked({"processFoo"}) final ExampleClass exampleclass)
{
new Expectations() {{
exampleclass.processFoo(); times = 1;
}};
exampleclass.example(true);
}
This should mock the processFoo method, leaving everything else intact, and checks to make sure it is called exactly once.

Don't consider doing any kind of partial mocking for this, all you're doing in that case is ensuring that if you want to refactor your code your tests will fail. There is a mantra in unit testing - "never test private methods".
What you should be doing is testing that the method you call conforms to the behaviour you want to see. In this case what happens when foo is true is what's important, not that it calls processFoo. So if foo is true you want to be testing that the action processFoo carries out is true and nothing else.

Related

How to test void method with private method calls using Mockito

I have the following piece of code
public class A extends B {
private boolean workDone = false;
#Override
public void publicMethod(boolean flag) {
if (!workDone) {
privateMethod();
workDone = true;
}
super.publicMethod(flag);
}
private void privateMethod() {
// some logic here
}
}
I'm new to mocking. I have following doubts. I'm trying to test the public method.
is it possible for me to assert the value of private variable workDone?
is it possible to verify the method call in the super class?
How can I mock the private method call in the method?
If you really want to verify it, you need to change your A class and extract the super call into a private method:
public class A extends B {
private boolean workDone = false;
#Override
public void publicMethod(final boolean flag) {
if (!workDone) {
privateMethod();
workDone = true;
}
callParentPublicMethod(flag);
}
private void callParentPublicMethod(final boolean flag) {
super.publicMethod(flag);
}
private void privateMethod() {
System.out.println("A: privateMethodCalled");
}
}
after this is done you can use PowerMock to verify private method invocations:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
#RunWith(PowerMockRunner.class)
#PrepareForTest({ A.class })
public class ATest {
#Test
public void publicMethod_test_false() throws Exception {
A spy = PowerMockito.spy(new A());
spy.publicMethod(false);
PowerMockito.verifyPrivate(spy).invoke("privateMethod");
PowerMockito.verifyPrivate(spy).invoke("callParentPublicMethod", false);
}
#Test
public void publicMethod_test_true() throws Exception {
A spy = PowerMockito.spy(new A());
spy.publicMethod(true);
PowerMockito.verifyPrivate(spy).invoke("privateMethod");
PowerMockito.verifyPrivate(spy).invoke("callParentPublicMethod", true);
}
}
Hope this helps.
When doing unittesting we verify the public observable behavior of the code under test. This is the return values delivered by the CUT and its communication with dependencies.
The private variable and the private methods inside the CUT are implementation details we don't want to test (explicitly) because we want them to be changeable without braking our test.
In rare cases the call to super class methods can be considered as "communication with dependency". In that case you create a spy() of the CUT. But usually this should be considered implementation detail too...
No. Private variables cannot be accessed other than via reflection. Which is usually not preferred, especially in unit tests.
Yes, assuming you get some assertable change do to super class method call. Like the change in the boolean flag
Yes you can using powermockito. For more see here.
Testing Private method using mockito

JUnit test if else case

How to write test to current method? I use jUnit 4.
public void setImage() {
if(conditionOne){
myView.setImageOne();
} else {
myView.setImageTwo();
}
}
You need to write two tests to cover both the scenarios as below:
import org.junit.Test;
public class SetImageTest {
#Test
public void testSetImageForConditionOne() {
//write test to make conditionOne true
}
#Test
public void testSetImageForElseCondition() {
//write test to make conditionOne false
}
}
Okay... there is a flaw in the way you wrote this method. There is something called testable code. Here is a link (how to write testable code and why it matters) that discusses testable code.
The method you wrote above is non-deterministic. Which means the method can exhibit different behaviors on different runs, even if it has the same input. In your case you have no input.
Currently, your original method is based on the environment of the method and not the input. This practice can make it very difficult and in some cases impossible to write proper test for your code.
So this is how you want it to look like...
public void setImage(boolean conditionOne) {
if(conditionOne){
myView.setImageOne();
} else {
myView.setImageTwo();
}
}
Now that the test is deterministic your either going to have to test the variables that are in the environment, or have a return statement.
So (adding a return statement) you can do this.
public static void setImage(boolean conditionOne, type myView) {
if(conditionOne){
myView.setImageOne();
} else {
myView.setImageTwo();
}
return myView;
}
Now your test can look something like this
public class SetImageTest {
#Test
public void testSetImage() {
type myViewOrig;
//define myViewOrig
type myView1;
//define myView1
type myView2;
//define myView2
assertEquals(setImage(<true>, type myViewOrig), myView1);
assertEquals(setImage(<false>, type myViewOrig), myView2);
}
}
Or you can just test the myView object after running your setImage method.

Verify that a private method was NOT executed JMockit

I have looked around for this question a bit and have not found exactly what I need. I have learned a bit more about JMockit and mocking. Which is good. Seems like everyone wants to know how to make sure something has executed. I would like to learn the opposite.
Ok - I am writing a test to check a not so happy path in a public method. The method that is being tested is void so I can't really assert the results. What I would like to do is verify that a method was NOT executed in this test case.
For example:
class ClassToTest {
private void method(String arg){}
public void publicMethod(String arg0, String arg1){
if(false){
//this method should never get called.
method(arg0);
}
}
}
class TestingClass{
#Tested
private ClassToTest classToTest = new ClassToTest();
#Test
public void testCheckingIfPrivateMethodWasCalled(){
classToTest.publicMethod("string1", "string2");
new Verifications() {
{
//At this point I am trying something like
Deencapsulation.invoke(classToTest, "method", "string1");
times = 0; //Also tried maxTimes = 0;
//Through debug it looks like the invoke is doing what it's named...
//invoking the private method, I don't want to invoke.
//How do I check that ClassToTest#method was not called?
}
}
}
}
What I am getting as results for the test case is a java.lang.IllegalStateException: Missing invocation to mocked type at this point; please make sure such invocations appear only after the declaration of a suitable mock field or parameter. Which is on the line of times = 0;.
I know the invoke is executing the private method. I am left scratching my head trying to figure out how to "check" that said method is called with out invoking/executing it.
Thanks.
One way of doing it is with the MockUp API for faking:
import static org.junit.Assert.assertFalse;
import org.junit.Test;
import mockit.Mock;
import mockit.MockUp;
import mockit.Tested;
public class TestingClass {
#Tested
private ClassToTest classToTest = new ClassToTest();
#Test
public void testCheckingIfPrivateMethodWasCalled() {
PrivateMethodCheckMockUp mockUp = new PrivateMethodCheckMockUp() {
#Mock
private void method(String arg) {
calledPrivate = true;
}
};
classToTest.publicMethod("string1", "string2");
assertFalse(mockUp.calledPrivate);
}
class PrivateMethodCheckMockUp extends MockUp<ClassToTest> {
boolean calledPrivate = false;
#Mock
private void method(String arg) {
calledPrivate = true;
}
}
}

Can I use an If statement to check which method made the call?

I want to make an if statement that checks to see which method made the call to a secondary method.
I will write what i want in pseudo code so you can see what I mean.
public static void methodOne() {
methodToCall();
}
public static void methodTwo() {
methodToCall();
}
public static void methodThree() {
methodToCall();
}
public static void methodToCall() {
if (methodOne made the call == true) {
execute this
} else if (methodTwo made the call == true){
execute this
} else if (methodThree made the call == true){
execute this
} else {
System.out.println("How did you get here?");
}
}
That's about the gist of it. I want a simple check to see which method made the call so I can choose which operation is relevant to the call.
Is this possible?
If it is not possible, is there a work around?
This is called 'state orientation', and it was debated extensively in the 1970s, possibly even the 1960s. The conclusion was that if you need to know this sort of thing you are already doing something seriously wrong, by introducing a two-way dependency into the code. What happens for example when you add another caller?
Use three short methods, instead of combining the logic of three short methods into one larger method. Once the short methods are created Just call the appropriate method from each calling method.
public static void methodOne() {
methodToCall1();
}
public static void methodTwo() {
methodToCall2();
}
public static void methodThree() {
methodToCall3();
}
public static void methodToCall1() {
int x = 0;
x = x - 3; //some custom logic to prep argument
commonLogic(x);
}
public static void methodToCall2() {
//Method 2 logic
int x = 0;
x = x + 3; //some custom logic to prep argument
commonLogic(x);
}
public static void methodToCall3() {
//Method 3 logic
int x = 0;
x = x * 3; //some custom logic to prep argument
commonLogic(x);
}
public static void commonLogic(int arg1){
//peform common logic
}
If these three methods would contain duplicate code, abstract the duplicate code into another method then call that method from within each of the smaller methods. The idea is to prepare the arguments to call the common function in each of the three smaller functions, then call the common function with those arguments.
A great deal of the abstraction afforded by methods comes from the fact that they do not need to know who is calling them, so the answer to your question is "no". It does not mean that you cannot make it work, though: make the callers pass some sort of a token (say, an enum value) identifying themselves to the callee. This would let you dispatch on that identity inside your method's implementation:
enum CallerContext {CALLER1, CALLER2, CALLER3};
...
public static void methodToCall(CallerContext context) {
...
}
This is not the most Object-Oriented way of doing things, however: very often, a better approach would be letting the callers supply the logic to be executed, rather than supplying a token identifies that logic. See Visitor Pattern for details on that approach.
You can do it by examining the call stack via Thread.getStackTrace():
public static void methodToCall(Action action) {
String callingMethod = Thread.currentThread().getStackTrace()[2].getMethodName();
if (callingMethod.equals("methodOne")) {
execute this0
} else if (callingMethod.equals("methodTwo")) {
execute this
} else if (callingMethod.equals("methodThree")) {
execute this
} else {
System.out.println("How did you get here?");
}
}
but you shouldn't - it's a bit anti-OO. Instead, change your method signature to something like this:
public enum Action {ONE, TWO, THREE}
public static void methodToCall(Action action) {
if (action == ONE) {
execute this
} else if (action == TWO) {
execute this
} else if (action == THREE) {
execute this
} else {
System.out.println("How did you get here?");
}
}
If you end up using an enum, then make sure to take advantage of the fact that enums in Java are no less than singleton instances of classes. Therefore you can declare the method as abstract in the enum definition and then override it in each instance, instead of passing the enum as a paramater to some method defined outside of the enum's context.
So it would look something like:
enum Method {
Mode1 {
#Override
void call() {
// do stuff
}
}, Mode2 {
#Override
void call() {
// do stuff differently
}
}, Mode3 {
#Override
void call() {
// do stuff even more differently
}
};
abstract void call();
}
And then you either don't need your wrapping methods, or, if they were supposed to do anything more, you write:
public static void methodOne() {
// some code
Method.Mode1.call();
// some code
}

Variable output Mockito mocks

I have a class WidgetProcessor that has a dependency on another class, FizzChecker:
public class FizzChecker {
public boolean hasMoreBuzz() {
// Sometimes returns true, sometimes returns false.
}
}
This hasMoreBuzz() method is invoked from inside WidgetProcessor like so:
public class WidgetProcessor {
public int process() {
while(fizzChecker.hasMoreBuzz()) {
// ... process stuff in here
}
}
}
I want to write test cases for when:
fizzChecker.hasMoreBuzz() returns false the 1st time it is called (hence the loop never executes)
fizzChecker.hasMoreBuzz() returns false on the 5th time it is called
I'm trying to figure out how to accomplish this with Mockito. So far my best (terrible) attempt:
WidgetProcessor fixture = new WidgetProcessor();
FizzChecker mockFizzChecker = Mockito.mock(FizzChecker.class);
// This works great for the first test case, but what about the 2nd
// where I need it to return: true, true, true, true, false?
Mockito.when(mockFizzChecker).hasMoreBuzz().thenReturn(false);
fixture.setFizzChecker(mockFizzCheck);
fixture.process();
// Assert omitted for brevity
Thanks in advance.
You can pass in multiple values to thenReturn, or keep chaining. Successive calls to the stubbed method will return the actions in sequence, repeating the final action for all calls. Examples:
// will return true four times, and then false for all calls afterwards
when(mockFizzChecker.hasMoreBuzz()).thenReturn(true, true, true, true, false);
when(mockFizzChecker.hasMoreBuzz())
.thenReturn(true)
.thenReturn(true)
.thenReturn(true)
.thenReturn(true)
.thenReturn(false);
// you can also switch actions like this:
when(someOtherMock.someMethodCall())
.thenReturn(1, 2)
.thenThrow(new RuntimeException());
You'll probably want to set them up separately:
public class WidgetProcessorTest {
private WidgetProcessor processor;
private FizzChecker mockFizzChecker;
#Before public void setUp() {
processor = new WidgetProcessor();
mockFizzChecker = Mockito.mock(FizzChecker.class);
processor.setFizzChecker(mockFizzChecker);
}
#Test public void neverHasBuzz() {
when(mockFizzChecker.hasMoreBuzz()).thenReturn(false);
processor.process();
// asserts
}
#Test public void hasFiveBuzzes() {
when(mockFizzChecker.hasMoreBuzz())
.thenReturn(true, true, true, true, false);
processor.process();
// asserts
}
}
Last note: In reality, you may find you need to coordinate multiple calls (such as hasMoreBuzz and getNextBuzz). If it starts to get complicated, and you foresee writing this in a lot of tests, consider skipping Mockito and instead just implementing a FakeFizzChecker.
Try using Answers. This will allow you to execute code when the hasMoreBuzz() method is called.
Look at the example in the link provided above. If you create an Answer object, and implement the answer() method to keep a counter, you can take action based on the value of that counter.
Edit: I wrote a quick test program to verify this. Here it is:
package com.ejk;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.mock;
public class SO {
#Test
public void testIt() {
IFoo mock = mock(IFoo.class);
MyAnswer myAnswer = new MyAnswer();
when(mock.doFoo()).then(myAnswer);
for (int i=1; i<10; i++) {
System.out.println(i+ ") " + mock.doFoo());
}
}
class MyAnswer implements Answer<Boolean> {
int counter = 1;
#Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
return (counter++ == 5) ? Boolean.FALSE : Boolean.TRUE;
}
}
interface IFoo {
boolean doFoo();
}
}

Categories

Resources