How to use JUnit to get database connection in spring?
I am trying to use the correct logic of services and methods to test the correct information provided by dB In this simple example, I only use the statement assert equals to compare the IDS provided for roleservice, but I still encounter an error I have the following code:
[update]
Test method:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(value = { "classpath:applicationContext.xml" }) @Transactional @WebAppConfiguration @ComponentScan(basePackages ={ "com.project.surveyengine" },excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = Configuration.class),@ComponentScan.Filter(type = FilterType.ANNOTATION,value = WebConfig.class)}) public class RoleServiceTest { @Configuration static class ContextConfiguration { @Bean public RoleService roleService() { RoleService roleService = new RoleService(); // set properties,etc. return roleService; } } private final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig() .setDefaultHighRepJobPolicyUnappliedJobPercentage(100)); private Closeable closeable; @Before public void setUp() { helper.setUp(); ObjectifyService.register(Role.class); closeable = ObjectifyService.begin(); } @After public void tearDown() { closeable.close(); helper.tearDown(); } @Autowired private RoleService roleService; @Test public void existsRole() throws Exception{ Role role = roleService.getByName("ROLE_ADMIN"); assertEquals("Correct test",Long.valueOf("5067160539889664"),role.getId()); } }
Roleservice is a service class that uses objectifyservice in Google App Engine to query database information
applicationContext. In XML
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config /> <context:component-scan base-package="com.project.surveyengine.config"/> <!--Controllers--> <bean id="in@R_685_2419@Controller" class="com.project.surveyengine.controller.In@R_685_2419@Controller" autowire="byType"></bean> <bean id="mailController" class="com.project.surveyengine.controller.MailController" autowire="byType"></bean> <bean id="loginController" class="com.project.surveyengine.controller.LoginController" autowire="byType"></bean> <bean id="initController" class="com.project.surveyengine.controller.InitController" autowire="byType"></bean> <!--Services--> <bean id="answerService" class="com.project.surveyengine.service.impl.AnswerService" autowire="byType"></bean> <bean id="blobService" class="com.project.surveyengine.service.impl.BlobService" autowire="byType"></bean> <bean id="mailService" class="com.project.surveyengine.service.impl.MailService" autowire="byType"></bean> <bean id="caseService" class="com.project.surveyengine.service.impl.CaseService" autowire="byType"></bean> <bean id="roleService" class="com.project.surveyengine.service.impl.RoleService" autowire="byType"></bean> </beans>
Go to com project. surveyengine. Config I have the following three classes:
1)
public class MyXmlWebApplicationContext extends XmlWebApplicationContext { protected void initBeanDeFinitionReader(XmlBeanDeFinitionReader beanDeFinitionReader) { super.initBeanDeFinitionReader(beanDeFinitionReader); if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) { beanDeFinitionReader.setValidating(false); beanDeFinitionReader.setNamespaceAware(true); } } }
2)
@Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter{ @Bean UrlBasedViewResolver resolver(){ UrlBasedViewResolver resolver = new UrlBasedViewResolver(); resolver.setPrefix("/views/"); resolver.setSuffix(".jsp"); resolver.setViewClass(JstlView.class); return resolver; } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/statics/**").addResourceLocations("/statics/"); } }
3)
@Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class SecurityContext extends WebSecurityConfigurerAdapter { @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/statics/**"); } @Override protected void configure(HttpSecurity http) throws Exception { http .formLogin() //...more code } }
RoleServiceTest.java:111 = assertEquals("Correct test",role.getId());
Solution
Do you try to follow the document http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/context/web/WebAppConfiguration.html Add @ webappconfiguration as suggested in?
I will test your test code in this way
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(value = { "classpath:applicationContext.xml" }) @TransactionConfiguration(transactionManager="txMgr",defaultRollback=false) @Transactional @WebAppConfiguration @ComponentScan(basePackages ={ "com.project.surveyengine" },excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION,value = Configuration.class) }) public class RoleServiceTest { @Autowired private RoleService roleService; @Test public void existsRole() throws Exception{ Role role = roleService.getByName("ROLE_ADMIN"); assertEquals("Correct test",role.getId()); } }
As you can see, I also added the comment @ webappconfiguration
I hope this can help you