Sunday, March 27, 2016

javax.servlet.ServletException: Circular view path []: would dispatch back to the current handler URL [/] again - Junit

While running a Standalone Junit Test for Spring Controller, sometimes you may incur the below exception, and your test will fail. This is because, when you don't declare or configure the ViewResolver in UnitTest, Spring registers a default InternalResourceViewResolver which creates instances of JstlView for rendering the View.

javax.servlet.ServletException: Circular view path [addExpense]: would dispatch back to the current handler URL [/addExpense] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
 at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(
 at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(
 at org.springframework.web.servlet.view.AbstractView.render(
 at org.springframework.web.servlet.DispatcherServlet.render(

To resolve the issue, we need to configure the ViewResolver, in the setup method of the Test class, Below is the sample code to do that. In the setup method, i have initialized the InternalResourceViewResolver, and assigned it to the viewResolver of standaloneSetup of MockMvcBuilder, this will resolve the issue.

public class ExpenseControllerTest {
    private ExpenseController expenseController;
    private MockMvc mockMvc;

    public void setup() {
 // Process mock annotations

        // Setup Spring test in standalone mode
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        this.mockMvc = MockMvcBuilders.standaloneSetup(expenseController).setViewResolvers(viewResolver).build();       

    public void testAddExpense() throws Exception{

Happy Programming.

Tuesday, March 15, 2016

Error: Could not find or load main class - Eclipse

When your eclipse project libraries are not pointing to JDK, and instead it is pointing to the JRE, you will get this error "Error: Could not find or load main class". If the JDK path was not configured in your installed JRE's please do the following.

From Eclipse Click File > Properties, select Java Build Path and select Libraries Tab.
In the Libraries Tab, select the JRE system library and click Edit, which will pop up a window.

Then click Installed JREs, button, which will pop up a window, which will display the JRE installed, as below.

Now Click Add, and provide the path, where your java jdk have been installed. In the below image, i have configured the path, where my Java JDK has been installed. Once set the path, it will fetch all the JRE libraries under that folder, as shown below.

Once you clicked finish button, the JDK entry will be shown in the Installed JREs window. Now select the newly added path and click apply. Now in the JRE System library window, Click on the Alternate JRE dropdown, it will list the jdk which was added by us, select that and click finish. This will change your JRE System library point to the JDK, what we have installed. Now select the xsd file and generate the JAXB classes, it will generate the classes successfully without any error.

Happy Programming.