Mein Projekt verwendet spring-boot
(1.4.0.release) und mybatis-spring-boot-starter
. Wenn ich versuche, einige Test-Code für den Controller zu haben, habe ich immer eine Ausnahme@WebMvcTest mapperscan Konflikt
Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
at org.springframework.util.Assert.notNull(Assert.java:115)
at org.mybatis.spring.support.SqlSessionDaoSupport.checkDaoConfig(SqlSessionDaoSupport.java:75)
at org.mybatis.spring.mapper.MapperFactoryBean.checkDaoConfig(MapperFactoryBean.java:74)
at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 42 more`
Aber wenn ich @MapperScan("com.toby.mapper")
kommentieren, es läuft sehr gut. Hier ist meine Beispielklasse:
@MapperScan("com.toby.mapper")
@EnableTransactionManagement
@EnableConfigurationProperties(AppConfig.class)
@SpringBootApplication(scanBasePackages = "com.toby.configuration,com.toby.web.controller,com.toby.service,com.toby.dao")
public class Example {
public static void main(String[] args) throws Exception {
//new SpringApplicationBuilder().sources(Example.class).run(args);
SpringApplication application=new SpringApplication(Example.class);
application.addInitializers(new PropertyPasswordDecodingContextInitializer());
application.run(args);
}
}
Hier mein Testcode ist:
package com.toby.web.controller;
import com.toby.common.config.AppConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
/**
* Created by Toby on 2016/8/10.
*/
@RunWith(SpringRunner.class)
@WebMvcTest(value = MyRestController.class)
public class MyRestControllerTests {
@Autowired
private MockMvc mvc;
@MockBean
private AppConfig appConfig;
@Test
public void testHome() throws Exception {
/*this.mvc.perform(get("/users").accept(MediaType.TEXT_PLAIN))
.andExpect(status().isOk()).andExpect(content().string("Honda Civic"));*/
}
}
Was sind die Abhängigkeiten von 'MyRestController'? Spring Boot wird diese Klasse nur als eine Bean registrieren, also muss alles, auf das Sie im Controller angewiesen sind, entweder bereitgestellt oder verspottet werden. –
MyRestController nur eine sehr einfache Demo; nur abhängig von @Autowired private AppConfig appConfig; und haben auch einen anderen Controller von Datenbank bezogenen Ressourcen abhängen –
Das Problem ist, dass ich @WebMvcTest (value = MyRestController.class) im Testcode angeben. Warum wurde ein anderer Controller ausgeführt? –