1

This is my first try with JUnit testing and Spring MVC. I have made a test configuration class for beans and one test class for controller. While executing the test, there is test failure which says Initialization error and no class definition found error. Following is my TestBeanConfig.java file

package com.mkyong.controller;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
@Configuration
@ComponentScan(basePackages = "com.mkyong.controller")
public class TestBeanConfig {

    @Bean
    MultiplicationService multiplicationservice() {
    return new MultiplicationService();
    }

    @Bean
    NumberDAO numberDao() {
    NumberDAO numberdao =new NumberDAO();
    return numberdao;
    }
}

and MultiplicationServiceTest.java

package com.mkyong.controller;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes={TestBeanConfig.class})
public class MultiplicationServiceTest {

    @Autowired
    private MultiplicationService multiplytestobject;

    MockMvc mockMvc ;

    @Autowired
    private WebApplicationContext wac;

    public MultiplicationServiceTest() {

    }

    public MultiplicationService getMultiplytestobject() {
        return multiplytestobject;
    }

    public void setMultiplytestobject(MultiplicationService multiplytestobject) {
        this.multiplytestobject = multiplytestobject;
    }

    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void testHandleMultiply() throws Exception {
    mockMvc.perform(post("/multiply").param("number1","7").param("number2","7"))
    .andExpect((model().attribute("result", "49")))
    .andExpect(redirectedUrl("/multiply.jsp"));
    }

}

Trace of error:

Tests in error: 
  initializationError(com.mkyong.controller.MultiplicationServiceTest): Failed to instantiate [org.springframework.test.context.web.WebDelegatingSmartContextLoader]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: javax/servlet/ServletContext

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.626s
[INFO] Finished at: Sun Jan 28 15:40:26 IST 2018
[INFO] Final Memory: 16M/180M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.9:test (default-test) on project bmfinal: There are test failures.
[ERROR] 

Thank you.

1 Answer 1

1

Add this to maven pom.xml dependencies:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

This should provide classes project's searching for.

Let me know if it helps you out.

Sign up to request clarification or add additional context in comments.

20 Comments

It has solved the initialization error. However other error has appeared. Tests in error: testHandleMultiply(com.mkyong.controller.MultiplicationServiceTest): Failed to load ApplicationContext
this leads to tal different scenario. Did u used applicationContext.xml ?You've started from Mkyong tutorial isn'it? which one?
i did not use applicationContext.xml. i started spring mvc with mkyong counter webapp project. for testing i had referred some other sources.
are you using Eclipse or STS?
eclipse. After writing TestBeanConfig.java do i need to use applicationContext.xml for configuration?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.