mercredi 8 avril 2015

Unit testing Java class with Mockito mocks

I wrote 2 classes in Java. These are Developer and GroupManager. Developer has field private GroupManager supervisor (with getter and setter) describing who hires him. What is more, GroupManager has following method:



public class GroupManager {

//...

private List<Developer> employees;

public void hire(Developer e) {
if(canHire()) {
if(employees.contains(e)) {
System.out.println(e.getName() + " is already hired");
}
else {
e.setSupervisor(this);
employees.add(e);
System.out.println(getName() + " is now hiring " + e.getName());
}
}
else {
System.out.println(getName() + "cannot hire more employees.");
}
}

}


This class exposes method hire which set supervisor on given developer. I would like to write tests for class Developer. I did it like this:



@Test
public void hiringWorksAsItShould() throws Exception {
GroupManager gm = new GroupManager("John Doe", "manager", 1);
gm.hire(developer);
assertThat(developer.getSupervisor()).isInstanceOf(GroupManager.class);
}


I am using FEST testing framework but since it is very intuitive I belive everyone can understand what it means. This test passes, but because I want it to be unit test a decided to replace = new GroupManager with = mock(GroupManager.class). The problem is that now this test fails. How could I correct this Mockito feature to check is developer's supervisor is instance of GroupManager.


EDIT The error I get:



java.lang.AssertionError: expecting actual value not to be null
at org.fest.assertions.Fail.failure(Fail.java:228)
at org.fest.assertions.Fail.fail(Fail.java:167)
at org.fest.assertions.Fail.failIfActualIsNull(Fail.java:100)
at org.fest.assertions.GenericAssert.isNotNull(GenericAssert.java:238)
at org.fest.assertions.ObjectAssert.isInstanceOf(ObjectAssert.java:52)
at DeveloperTest.hiringWorksAsItShould(DeveloperTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)


Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire