junit-team / junit4

A programmer-oriented testing framework for Java.
https://junit.org/junit4
Eclipse Public License 1.0
8.51k stars 3.24k forks source link

SpringBoot test failure in circleci #1734

Closed prajaktaNextdoor closed 2 years ago

prajaktaNextdoor commented 2 years ago

I have a class that I want to test using @SpringBootTest annotation. I have autowired the class under test in the test class. The class under test calls a factory.getInstance method which returns a real bean of required type. This bean has a dependency autowired. When I run my test using circleci, I do see this autowired dependency injected as a mock instance. This is weird as my test class is not mocking any beans.

class EligibilityServiceTest {

    @Autowired
    private EligibilityService service;

    @Test
    public void testSomeMethod() {
        Result result = service.pass() 
        assertTrue(result.success) // failed
    }
}
@Component
class EligibilityService {

    public Result pass() {
        for (Collection::stream).foreach(obj -> {
            DummyClass instance = Factory.getInstance(obj);
            instance.someMethod(); // this returns unexpected value since the dependency in the instance is being mocked when run 
            // with the whole test suite in circleci.
        });
    }
}
@Component
class Factory {

    private static DummyClass dummy;

    private static  DummyClass1 dummy1;

    @Autowired
    private DummyClass dummyInstance;

    @Autowired
    private  DummyClass1 dummyInstance1;

    @PostContruct
    public void init() {
        this.dummy = dummyInstance;
        this.dummy1 = dummyInstance1;
    }

    public static AnInterface getInstance(Obj objType) { 
        switch(objType) {
            case1:
                return this.dummy;
            csse2:
                return this.dummy1;
        }
    }
}
@Component
class DummyClass implements AnInterface {
  @Autowired
  private SampleBean bean;
  public boolean someMethod() {
     // ...logic
  }
}
@Component
class DummyClass1 {
  @Autowire
  private SampleBean bean;
  public boolean someMethod() {
     // ...logic
  }
}

When I run the test on my local, I do not see the dependency as mocked. It is a real instance as expected. Could you please guide me in why this dependency may be getting mocked in circleci run?

Thanks,

Praj

sbrannen commented 2 years ago

I've edited your comment to improve the formatting. You might want to check out this Mastering Markdown guide for future reference.

sbrannen commented 2 years ago

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. We prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.


Aside from the fact that the code you supplied does not compile, the issue you are describing sounds like it is related to Spring Boot instead of JUnit.

prajaktaNextdoor commented 2 years ago

The code was just a pseudo code to give an overview of the scenario I am testing. It was not meant to be a production ready code. Thanks.