错误无法在jUnit Testing Spring Integration中打开ServletContext资源

时间:2018-12-20 06:19:50

标签: junit spring-integration junit4

我是Spring Integration的新手,我正在尝试测试该频道。 我写了下面的代码。但是我在测试时遇到异常。请帮助我解决这个问题。

Spring集成流程。

<int:channel id="reqGetAllMedChannel" />
<int:channel id="resGetAllMedChannel" />
<int-http:inbound-gateway
    request-channel="reqGetAllMedChannel"
    reply-channel="resGetAllMedChannel" supported-methods="GET"
    path="/getAllMedicines">

    <int-http:request-mapping
        consumes="application/json" produces="application/json" />
</int-http:inbound-gateway>


<int:service-activator
    ref="medicineServiceActivator" method="getAllMedicines"
    input-channel="reqGetAllMedChannel"
    output-channel="resGetAllMedChannel" />

正在被调用的方法。

public Message<List<Medicine>> getAllMedicines() {
    log.info("GET request for Medicine");
    List<Medicine> medLst = medicineService.getAllMedicines();
    return MessageBuilder.withPayload(medLst).setHeader("http_statusCode", HttpStatus.OK).build();
}

测试xml配置

<import resource="classpath:medicineIntegration.xml"/>

<int:bridge input-channel="resGetAllMedChannel" output-channel="testOutput">
</int:bridge>

<int:channel id="testOutput">
<int:queue/>
</int:channel>

jUnit测试用例。

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(locations= {"classpath*:Integration-Test.xml"})
public class MessageChannelTest {

@Autowired
private MessageChannel inputChannel;

@Autowired
private QueueChannel testChannel;

@Mock
private MedicineService medicineService;

@Test
public void testChannel() {
    List<Medicine> medicines = new ArrayList<Medicine>();
    Medicine medicine1 = new Medicine();
    medicine1.setMedName("Crocine");
    medicine1.setExpDate("01-01-2020");
    Medicine medicine2 = new Medicine();
    medicine2.setMedName("Paracetamole");
    medicine2.setExpDate("02-02-2020");
    medicines.add(medicine1);
    medicines.add(medicine2);
    when(medicineService.getAllMedicines()).thenReturn(medicines);
    inputChannel.send(MessageBuilder.withPayload("").build());
    Message<?> outMsg = testChannel.receive(1000);
    assertThat(medicineService).isEqualTo(outMsg.getPayload());
}
}

以下是我在测试jUnit代码时遇到的异常。请帮助我,让我知道我做错了什么。

java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) ~[junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) ~[junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) ~[junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) ~[junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) ~[junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) ~[junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) ~[junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) ~[spring-test-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89) ~[.cp/:na]
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) ~[.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) ~[.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) ~[.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) ~[.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209) ~[.cp/:na]

2 个答案:

答案 0 :(得分:1)

根据例外情况,您的olMailItem取决于medicineService。由于您只有一个:

MedicineRepository

您不会影响应用程序上下文,也不会替换您的@Mock private MedicineService medicineService; bean定义。

如果您真的只想模拟medicineService并在应用程序上下文中替换它,则应考虑使用Spring Boot中的MedicineServicehttps://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-testing-spring-boot-applications-mocking-beans

对于该@MockBean,您也可以@MockBean,而您的MedicineRepository在应用程序上下文中将保持不变。

在其他问题中,我已经向您建议过类似的内容:JUnit for Spring Integration Activator with return type Message<?>

答案 1 :(得分:0)

MedicineRepository是模拟服务的一部分,请尝试使用@Spy@Autowired将其添加到测试上下文中:

@Autowired
@Spy
MedicineRepository medicineRepository