0

I have controller

@Controller
public class AuthorController {

    @Autowired
    private AuthorDAO authorDao;

    @RequestMapping("/authors")
    public String showAuthor(@RequestParam String name, ModelMap model) {
        Author author = authorDao.findByName(name);
        model.addAttribute("author", author);

        return "authors";
    }

}

I wrote test for it

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:test-application-context.xml"})
public class AuthorControllerTest {

    private static final String JACK_C = "Jack C.";

    @Autowired
    AuthorController controller;

    @Test
    public void testShowAuthor() {

        Author expectedAuthor = new Author();

        AuthorDAO daoMock = mock(AuthorDAO.class);

        when(daoMock.findByName(JACK_C)).thenReturn(expectedAuthor);

        ModelMap model = new ModelMap();

        String view = controller.showAuthor(JACK_C, model);
        assertEquals("View name is incorrect","authors", view);

        assertSame(expectedAuthor, model.get("author"));
        verify(daoMock).findByName(JACK_C);
    }

}

test-application-context.xml:

<context:annotation-config />
    <context:component-scan base-package="com.github.futu" />   

     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property 
        name="url" value="jdbc:mysql://localhost:3306/blog" /> <property name="username" 
        value="blogger" /> <property name="password" value="blogger" /> </bean>

    <bean id="com.github.futu.dao.AuthorDAO" class="com.github.futu.dao.impl.AuthorDAOXml"/> 

    <bean id="com.github.futu.dao.PostDAO" class="com.github.futu.dao.impl.PostDAOXml" />       

    <bean id="validator" class="com.github.futu.validator.PostValidator" /> 

But real dao is called. What have I missed?

Constantine Gladky
  • 1,245
  • 6
  • 27
  • 45

1 Answers1

1

You're creating a mock here

AuthorDAO daoMock = mock(AuthorDAO.class);

that is completely unrelated to your controller injected into your test class

@Autowired
AuthorController controller;

Of course the autowired AuthorDao target is going to come from your XML configuration

@Autowired
private AuthorDAO authorDao;

Ideally you would change your XML configuration only produce a @Controller bean and add a setter to it to set the AuthorDao from within the test, using your mock.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I got it. But I can't change my controller. Maybe reflection helps me – Constantine Gladky Dec 20 '13 at 14:33
  • @ConstantineGladky There are a few alternatives. [Here's](http://stackoverflow.com/questions/13870371/how-do-i-inject-mocks-into-a-spring-class-marked-as-transactional) one. [Here's](http://stackoverflow.com/questions/4278788/injecting-mock-beans-into-spring-context-for-testing) one more. And [finally](http://stackoverflow.com/questions/2457239/injecting-mockito-mocks-into-a-spring-bean). – Sotirios Delimanolis Dec 20 '13 at 14:34