Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easymock tests fails when Object property is used #217

Open
klinki opened this issue Jun 11, 2018 · 1 comment
Open

Easymock tests fails when Object property is used #217

klinki opened this issue Jun 11, 2018 · 1 comment

Comments

@klinki
Copy link

klinki commented Jun 11, 2018

Hello,

we have encountered very strange bug in Easymock.
It fails to initialize @Autowired dependencies, when Object property is used.

We have following code:

Dependency

package test.easymock.bug;

import org.springframework.stereotype.Component;

@Component
public class AutowiredDependency {
  public String sayHello(String name) {
    return "Hello World, "  + name + "!";
  }
}

Service

package test.easymock.bug;

import org.springframework.beans.factory.annotation.Autowired;

public class JustTestService {
  private final Object lock = new Object();

  @Autowired
  private AutowiredDependency dependency;

  public void foo(String name) {
    if (dependency == null) {
      throw new RuntimeException("Easymock error");
    }

    dependency.sayHello(name);
  }
}

Test for service

package test.easymock.bug;

import static org.easymock.EasyMock.expect;

import com.googlecode.easymockrule.EasyMockRule;
import com.googlecode.easymockrule.StrictMock;
import com.googlecode.easymockrule.TestSubject;
import org.junit.Test;
import org.junit.Rule;

public class JustTestServiceTest {
  @Rule
  public EasyMockRule mocks = new EasyMockRule(this);

  @TestSubject
  JustTestService testedService;

  @StrictMock
  AutowiredDependency dependencyMock;

  @Test
  public void testFoo() {
    String name = "Johnny";

    expect(dependencyMock.sayHello(name)).andReturn("Hi");
    mocks.replayAll();

    testedService.foo(name);
  }
}

it fails on dependency not being correctly initialized (it is null).
BUT if we change order of properties in JustTestService, so @Autowired properties are first, it suddenly starts working.

package test.easymock.bug;

import org.springframework.beans.factory.annotation.Autowired;

public class JustTestService {
  @Autowired
  private AutowiredDependency dependency;

  private final Object lock = new Object();

  public void foo(String name) {
    if (dependency == null) {
      throw new RuntimeException("Easymock error");
    }

    dependency.sayHello(name);
  }
}

what is even more strange, if we use different type of object (Integer for example) it also works.

package test.easymock.bug;

import org.springframework.beans.factory.annotation.Autowired;

public class JustTestService {
  private final Integer lock = new Integer();

  @Autowired
  private AutowiredDependency dependency;

  public void foo(String name) {
    if (dependency == null) {
      throw new RuntimeException("Easymock error");
    }

    dependency.sayHello(name);
  }
}
@henri-tremblay
Copy link
Contributor

It looks like a bug but I haven't had the time to have a look. Will do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants