0

JUnit テストを実行すると、JobConfigurationREST.startJob メソッドを呼び出そうとするたびに、Spring が JobRunner クラスの同じインスタンスを誤って使用します。

しかし、REST Web サービス呼び出しを介して呼び出すと、Spring は JobRunner クラスの別のインスタンスを提供してくれます。これは私が望んでいるものです。

startJob メソッドを呼び出すときに別のリクエストを処理していることを JUnit で Spring に伝えるにはどうすればよいですか?

次のコードがあります。

@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);
    }
}
4

0 に答える 0