Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to write junit test in Spring with JavaConfig

I am trying to write a junit test for my example project but don't know how to access the ApplicationContext in the jUnit Test:

Here is the main class of the project that works:

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() + ".");
        }
    }

Now here is the junit test:

@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");
    }
}
like image 579
SJS Avatar asked Apr 30 '13 12:04

SJS


People also ask

How do you write JUnit test cases for JPA repository?

You can create a @DataJpaTest and @Autowire your repository into it. For example: @RunWith(SpringRunner. class) @DataJpaTest public class MyJpaTest { @Autowired private ChartRepository chartRepository; @Test public void myTest() { ... } }


1 Answers

You can use autowiring. Note, most times you are not interested in the application context itself, but in one or more beans that are associated with it. Below are two examples that essentially does the same thing:

@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.
    }
}

See the reference docs for details.

like image 123
matsev Avatar answered Oct 21 '22 00:10

matsev