Done some REST API (Spring + Hibernate application), want to add some tests of controllers. Please help how start (@Before) and finish (@After) server? DataConfig.java:
@Configuration
@PropertySource("file:${properties.home}app.properties")
public class DataConfig {
@Autowired
private Environment env;
@Bean
public LocalSessionFactoryBean sessionFactory() {
Resource config = new ClassPathResource("hibernate.cfg.xml");
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setConfigLocation(config);
sessionFactory.setPackagesToScan(env.getProperty("HomeAutomation.entity.package"));
sessionFactory.setDataSource(dataSource());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(env.getProperty("HomeAutomation.db.driver"));
ds.setUrl(env.getProperty("HomeAutomation.db.url"));
ds.setUsername(env.getProperty("HomeAutomation.db.username"));
ds.setPassword(env.getProperty("HomeAutomation.db.password"));
return ds;
}
}
hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://ift.tt/1fnOghG">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="show_sql">true</property>
<property name="hibernate.implicit_naming_strategy">org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
app.properties:
HomeAutomation.entity.package = com.zzheads.HomeAutomation.model
HomeAutomation.db.driver = org.h2.Driver
HomeAutomation.db.url = jdbc:h2:tcp://localhost/~/data/HomeAutomation
HomeAutomation.db.username = sa
HomeAutomation.db.password =
Application.java:
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static final String BASE_URL ="http://localhost:8080/";
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
And how test will looks like (need help here):
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class RoomControllerTest {
@Autowired
private RoomService roomService;
@Autowired
private EquipmentService equipmentService;
@Autowired
private ControlService controlService;
private ApiClient client;
private Gson gson;
private static final String PORT = "4568";
private static final String TEST_DATASOURCE = "jdbc:h2:mem:testing";
@BeforeClass
public static void startServer() {
}
@AfterClass
public static void stopServer() {
}
@Before public void setUp() throws Exception {
}
@After public void tearDown() throws Exception {
}
@Test public void testAddRoom() throws Exception {
Map<String, Object> values = new HashMap<>();
values.put("roomName", "Kitchen");
values.put("squareFootage", "325");
ApiResponse res = client.request("POST", "/room", gson.toJson(values));
assertEquals(HttpStatus.CREATED.value(), res.getStatus());
}
Aucun commentaire:
Enregistrer un commentaire