0
這裏是我的服務類的JUnit Spring MVC的服務方法返回類型爲void
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.Message;
import org.springframework.integration.MessagingException;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.MessageHandler;
import org.springframework.stereotype.Service;
@Service
public class EmailService implements MessageHandler {
@Autowired
@Qualifier("receiveChannel")
private DirectChannel messageChannel;
private final Log logger = LogFactory
.getLog(EmailService.class);
public void init() {
logger.info("initializing...");
System.out.println("INIT");
messageChannel.subscribe(this);
}
@Override
public void handleMessage(Message<?> message) throws MessagingException {
logger.info("Message: " + message);
}
}
因爲我想創建一個JUnit測試案例初始化。怎麼寫?
這是我試過的。但它不工作
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration("classpath:webapptest")
@ContextConfiguration(locations = {"classpath:test-applicationcontext.xml"})
public class EmailServiceTest {
@Autowired
private EmailService emailService;
@Test(timeout=600000)
public void testEmailService() throws Exception {
emailService=Mockito.spy(emailService);
Mockito.doNothing().when(emailService).init();
}
}
在控制檯它不打印在init()
方法記錄器或打印語句。
我在做什麼錯誤?如何編寫測試用例?
謝謝。我如何驗證? – iCode
我加了上面的。它仍然不打印任何東西 – iCode
@iProgrammer那麼,你告訴它什麼都不做。印刷會做點什麼,不是嗎? –