title: java.lang.IllegalStateException: missing behavior
body: <p>I have a class that looks like this </p>
  
  <pre><code>public User createUser(User user, Set&lt;UserRole&gt; userRoles) throws DataAccessException {
          User localUser = userRepository.findByUsername(user.getUsername());
  
          if (localUser != null) {
              LOG.info("user {} already exists. Nothing will be done.", user.getUsername());
          } else {
              for (UserRole ur : userRoles) {
                  roleRepository.save(ur.getRole());
              }
              user.getUserRoles().addAll(userRoles);
  
              localUser = userRepository.save(user);
          }
  </code></pre>
  
  <p>I am writing a unit test on it and by the way my repositories are mocked but will not be included in the question, I keep getting errors </p>
  
  <pre><code>UserRepository.findByUsername(null)
  Usage is: expect(a.foo()).andXXX(), mergedContextConfiguration = [WebMergedContextConfiguration@6963b0bd testClass = LogicTest, locations = '{}', classes = '{class com.adminportal.config.SecurityConfig}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[org.springframework.test.context.web.socket.MockServerContainerContextCustomizer@48524010, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@5906ebcb, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@55a561cf, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@2cbb3d47], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]].
  </code></pre>
  
  <p>And this is the test, can someone tell me what's wrong I think its from the repository call to get username    </p>
  
  <pre><code>  @Test
       public void createUserTest() throws Exception {
           User user1 = new User();
  
  
  user1.setUsername("james");
          User user =  userRepository.findByUsername(user1.getUsername());
  
          Set&lt;UserRole&gt; userRoles = new HashSet&lt;&gt;();
          Role role1 = new Role();
  
   role1.setRoleId(3);
              role1.setName("ADMIN");
              userRoles.add(new UserRole(user1, role1));
  userRepository.save(user1);//stack trace show problem with this line
            expect(user1.getUsername()).andReturn("james");
          }
  </code></pre>
  
  <p>Thank you 
  Stack trace </p>
  
  <pre><code>    at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:42)
      at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:94)
      at com.sun.proxy.$Proxy115.save(Unknown Source)
      at com.admin.service.LogicTest.createUserTest(LogicTest.java:104)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
      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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
      at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
      at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
      at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
      at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  
  19:43:16.623 [Thread-3] INFO  o.s.w.c.s.GenericWebApplicationContext - Closing org.springframework.web.context.support.GenericWebApplicationContext@291ae: startup date [Wed Jan 03 19:42:51 MSK 2018]; root of context hierarchy
  19:43:16.628 [Thread-3] INFO  o.s.j.e.a.AnnotationMBeanExporter - Unregistering JMX-exposed beans on shutdown
  19:43:16.634 [Thread-3] INFO  org.dozer.jmx.JMXPlatformImpl - Unregistering existing Dozer JMX MBean [org.dozer.jmx:type=DozerAdminController].
  19:43:16.635 [Thread-3] INFO  org.dozer.jmx.JMXPlatformImpl - Unregistering existing Dozer JMX MBean [org.dozer.jmx:type=DozerStatisticsController].
  19:43:16.639 [Thread-3] INFO  o.s.o.j.LocalContainerEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'default'
  19:43:16.639 [Thread-3] INFO  o.h.tool.hbm2ddl.SchemaExport - HHH000227: Running hbm2ddl schema export
  19:43:16.702 [Thread-3] INFO  o.h.tool.hbm2ddl.SchemaExport - HHH000230: Schema export complete
  </code></pre>