Archived
0

замена конфигурации xml на class

This commit is contained in:
2018-09-07 20:50:20 +03:00
parent a6f4c42b4e
commit ef7bde7138
3 changed files with 40 additions and 30 deletions

View File

@@ -0,0 +1,38 @@
package mc.core.h2db;
import mc.core.world.World;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@Configuration
public class SpringConfig {
@Bean
public World mockWorld() {
World mockWorld = mock(World.class);
when(mockWorld.getName()).thenReturn("mock_world");
return mockWorld;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dmds = new DriverManagerDataSource();
dmds.setDriverClassName("org.h2.Driver");
dmds.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
dmds.setUsername("sa");
return dmds;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
}

View File

@@ -17,25 +17,18 @@ import java.sql.SQLException;
import java.util.UUID;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:springTest.xml"})
@ContextConfiguration(classes = {SpringConfig.class})
public class TestH2Database {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private World mockWorld;
private void createMockWorld() {
mockWorld = mock(World.class);
when(mockWorld.getName()).thenReturn("mock_world");
}
@PostConstruct
public void init() throws IOException {
jdbcTemplate.execute(IOUtils.resourceToString("/sqls/create_tables.sql", StandardCharsets.UTF_8));
createMockWorld();
}
private H2Player buildPlayer(final String name, final EntityLocation location) {

View File

@@ -1,21 +0,0 @@
<?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-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>