Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6237369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:00:55+00:00 2026-05-24T11:00:55+00:00

I have a Spring web application and I want to make unittests for my

  • 0

I have a Spring web application and I want to make unittests for my controllers. I have decided not to use Spring to setup my tests but to use Mockito mock objects in conjunction with my controllers.

I build and run tests with Maven2 and the surefire plugin. This is from my pom.xml

        <!-- Test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.framework.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>com.springsource.org.junit</artifactId>
            <version>4.5.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.0-rc1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

I setup my compiler and surefire plugins like this:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <verbose>true</verbose>
                    <compilerVersion>1.6</compilerVersion>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.4.3</version>
            </plugin>

My test class looks like this:

@RunWith(MockitoJUnitRunner.class)
public class EntityControllerTest {

private EntityController entityController;

private DataEntityType dataEntityType = new DataEntityTypeImpl("TestType");

@Mock
private HttpServletRequest httpServletRequest;

@Mock
private EntityFacade entityFacade;

@Mock
private DataEntityTypeFacade dataEntityTypeFacade;

@Before
public void setUp() {
    entityController = new EntityController(dataEntityTypeFacade, entityFacade);
}

@Test
public void testGetEntityById_IllegalEntityTypeName() {
    String wrong = "WROOONG!!";
    when(dataEntityTypeFacade.getEntityTypeFromTypeName(wrong)).thenReturn(null);
    ModelAndView mav = entityController.getEntityById(wrong, httpServletRequest);
    assertEquals("Wrong view returned in case of error", ".error", mav.getViewName());
}

Annotations all around 🙂

But when building from the commandline i get a NullPointerException in the line when(dataEntityTypeFacade.getEntityTypeFromTypeName(wrong)).thenReturn(null); as the dataEntityTypeFacade object is null. When I run my testcase in Eclipse all is well and my mock objects are instantiated and the method annotated with the @Before is called.

Why are my annotations seemingly ignored when running from the command line???

/Eva

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:00:55+00:00Added an answer on May 24, 2026 at 11:00 am

    Have you called:

     MockitoAnnotations.initMocks(testClass);
    

    in the base class or a test runner as mention here: http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#9

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a j2ee web application running on Spring framework. I want to implement
I have a web application developed with Spring 2.5. The application has numerous controllers,
I'm trying to make this 2-player web game application using Spring MVC. I have
I have an existing and working java web application. I want to make this
I have Spring web application. I would like to put some common piece of
I have a Spring Web application with an applicationContext.xml loaded through a ContextLoaderListener in
I have a Spring Web MVC application that I'd like to serve a large,
I have this web application using Spring Web Flow framework. In my main page
I have a localized spring mvc based web application, that has an externalized messages
I have a web application using spring annotations extensivley and I have my proguard

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.