title: JUnit test for a Step scoped Bean using StepScopeTestExecutionListener.class : Still getting "No Scope registered"
body: <p>I am trying to write an independent unit test case for a "step" scoped bean. I posted this <a href="https://stackoverflow.com/questions/61099235/writing-a-junit-test-for-a-step-scoped-bean-no-scope-registered-for-scope-na">question</a> earlier and understand that I need to use a <code>StepScopeTestExecutionListener</code> to create a step scope for my unit test; however, I still get the following exception even after using the <code>StepScopeTestExecutionListener</code> :</p>
  
  <pre><code>Caused by: java.lang.IllegalStateException: No Scope registered for scope name 'step'
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:343)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
  </code></pre>
  
  <p><strong>My JUnt</strong> </p>
  
  <pre><code>@TestExecutionListeners({ StepScopeTestExecutionListener.class,DependencyInjectionTestExecutionListener.class  })
  @RunWith(SpringJUnit4ClassRunner.class)
  @PropertySource("classpath:properties/common.properties")
  @ContextConfiguration(locations = { "/spring/common-context.xml" })
  public class ConfigDAOImplTest {
  
      @Autowired
      private ConfigDAOImpl configDAO;
  
      @Spy
      private ContextParamDAO contextParamDAO = new ContextParamDAOImpl();
  
      private static final String SCHEMA_CONFIG = "classpath:data/CONFIG_SCHEMA.sql";
      private static final String DATA_CONFIG = "classpath:data/CONFIG_DATA.sql";
  
      @Before
      public void init() {
          MockitoAnnotations.initMocks(this);
  
          DataSource dataSource = new EmbeddedDatabaseBuilder()
                  .setType(EmbeddedDatabaseType.H2)
                  .addScript(SCHEMA_CONFIG)
                  .addScript(DATA_CONFIG)
                  .build();
  
          JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
  
          //override the jdbcTemplate for the test case    
          configDAO.setJdbcTemplate(jdbcTemplate);
          configDAO.setContextParamDAO(contextParamDAO);
  
  
      }
  
      public StepExecution getStepExecution() {
  
          JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
          jobParametersBuilder.addString("test", "test");
          JobParameters jobParameters = jobParametersBuilder.toJobParameters();
  
          JobInstance jobInstance = new JobInstance(12345L,"testJob");
          JobExecution jobExecution = new JobExecution(jobInstance,jobParameters);
  
          StepExecution execution = 
          MetaDataInstanceFactory.createStepExecution(jobExecution,"step",11245L);
          execution.getExecutionContext().putString("input.data", "foo,bar,spam");
          return execution;
      }
  }
  </code></pre>
  
  <p>I can confirm that the <code>getStepExecution</code> does get called as I ran the unit test in debug mode with a  breakpoint on this method.</p>
  
  <p><strong>Question : Why is the step scope still not getting initialized for my JUnit?</strong></p>
  
  <p><strong>Note:</strong> On an unrelated  note, the reason why I am manually creating a <code>JobExecution</code> inside  <code>getStepExecution</code> instead of using <code>MetaDataInstanceFactory.createJobExecution</code> is because for some weird reason, the compiler is able to find the <code>createJobExecution</code> method but the method is not found at runtime. I used the <code>-verbose:class</code> flag and can confirm that there is only one jar from where the <code>MetaDataInstanceFactory</code> class gets loaded and that jar contains the required method as well. I am assuming that manually creating a <code>JobExecution</code> instance should not be the root cause of my issue.</p>