小编Ora*_*ran的帖子

尝试使用JavaConfig在Spring中编写junit测试

我正在尝试为我的示例项目编写junit测试,但不知道如何在jUnit Test中访问ApplicationContext:

以下是该项目的主要类:

public static void main(String[] args)
    {
        // in this setup, both the main(String[]) method and the JUnit method both specify that
        ApplicationContext context = new AnnotationConfigApplicationContext( HelloWorldConfiguration.class );
        MessageService mService = context.getBean(MessageService.class);
        HelloWorld helloWorld = context.getBean(HelloWorld.class);

        /**
         * Displaying default messgae
         */
        LOGGER.debug("Message from HelloWorld Bean: " + helloWorld.getMessage());

        /**
         *   Saving Message to database
         */
        Message message = new Message();
        message.setMessage(helloWorld.getMessage());
        mService.SaveMessage(message);

        /**
         * Settting new message in bean
         */
        helloWorld.setMessage("I am in Staten Island, New …
Run Code Online (Sandbox Code Playgroud)

junit spring spring-mvc

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

junit ×1

spring ×1

spring-mvc ×1