When I run my JUnit tests Spring mistakenly uses the same instance of my JobRunner class each time I try calling the JobConfigurationREST.startJob method.
However, when I call in via a REST web service call, Spring gives me a different instance of the JobRunner class, which is what I want.
How can I tell Spring in my JUnit that we're dealing with a separate request when I call the startJob method?
I have the following code:
@RestController
@RequestMapping("/jobConfiguration")
public class JobConfigurationREST extends RESTBase {
@Autowired
private WebApplicationContext context;
@RequestMapping(value="/startJob", method = RequestMethod.POST, produces="application/json")
@ResponseBody
public ResponseEntity<String> startJob(@RequestBody String jobConfigInfo) {
JobRunner jr = getJobRunner();
}
private JobRunner getJobRunner() {
return (JobRunner)context.getBean("jobRunner");
}
}
@Component
@Scope("request")
public class JobRunner{
}
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(loader = GenericXmlWebContextLoader.class, locations = {"classpath:/config/spring-config-test.xml"})
public class JobConfigurationRESTTest {
@Autowired
private JobConfigurationREST jcREST;
@Test
public void testSingletonBug() throws IOException{
String jobConfig;
ResponseEntity<String> responseJobStatus = jcREST.startJob(jobConfig);
ResponseEntity<String> responseJobStatus2 = jcREST.startJob(jobConfig);
}
}