【问题标题】:Trying to write junit test in Spring with JavaConfig尝试使用 JavaConfig 在 Spring 中编写 junit 测试
【发布时间】:2013-04-30 12:32:16
【问题描述】:

我正在尝试为我的示例项目编写一个 junit 测试,但不知道如何访问 jUnit 测试中的 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 York");
        LOGGER.debug("Message from HelloWorld Bean: " + helloWorld.getMessage());

        /**
         * Saving Message in database.
         */
        message.setMessage(helloWorld.getMessage());
        mService.SaveMessage(message);

        /**
         * Getting messages from database
         *    - display number of message(s)
         *    - display each message in database
         */
        List<Message> myList = mService.listMessages();
        LOGGER.debug("You Have " + myList.size() + " Message(s) In The Database");

        for (Message i : myList)
        {
            LOGGER.debug("Message: ID: " + i.getId() + ", Message: " + i.getMessage() + ".");
        }
    }

现在是junit测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = HelloWorldConfiguration.class)
public class TestApp {


    @Test
    public void testBean() {
        HelloWorld helloWorld = context.getBean(HelloWorld.class);

        helloWorld.setMessage("I Love Dogs");
        Assert.assertEquals(helloWorld.getMessage(), "I Love Dogs");
    }
}

【问题讨论】:

    标签: spring spring-mvc junit


    【解决方案1】:

    您可以使用自动装配。请注意,大多数时候您对应用程序上下文本身并不感兴趣,而是对与之关联的一个或多个 bean 感兴趣。下面是两个本质上做同样事情的例子:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = HelloWorldConfiguration.class)
    public class TestApp {
    
        @Autowired
        HelloWorld helloWorld;
    
        @Test
        public void testBean() {
            helloWorld.setMessage(...);
            // asserts, etc.
        }
    }
    

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = HelloWorldConfiguration.class)
    public class TestApp {
    
        @Autowired
        ApplicationContext applicationContext;
    
        HelloWorld helloWorld;
    
        @Before
        public void setUp() {
            helloWorld = context.getBean(HelloWorld.class);
        }
    
        @Test
        public void testBean() {
            helloWorld.setMessage(...);
            // asserts, etc.
        }
    }
    

    详情请参阅reference docs

    【讨论】:

      猜你喜欢
      • 2014-03-19
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 2015-10-04
      • 2011-12-03
      相关资源
      最近更新 更多