java - Spring boot doesn't process commit in junit when testing hibernate repository -
hi trying build junit test case @before setup after processing junit class, found no data changed in database , no exception throws well. funny thing can call controller , can save , delete calling service http client. below code, please help. lot.
@springapplicationconfiguration(classes = mockservletcontext.class) @webappconfiguration public class userrepositorytest extends abstractbestfirsttest { private mockmvc mvc; @autowired private userrepositoryi userrepo; private void userrepositorytestpreparer() { user user = new user(); user.setemail("markii@gmail.com"); user.setname("tony stark"); userrepo.deleteall(); userrepo.save(user); } @beforetransaction public void setupdata() { userrepositorytestpreparer(); } @before public void setup() { mvc = mockmvcbuilders.standalonesetup(new samplecontroller()).build(); } @after public void teardown() { // implement me } @transactional @commit @test public void testsave() { assert.assertequals("save failed", 1, userrepo.finduserbyname("tony stark").size()); assert.assertequals("save failed", 1, userrepo.finduserbyemail("markii@gmail.com").size()); } @test public void testrestfulbasic() throws exception { resultactions actions = mvc.perform(mockmvcrequestbuilders.get("/sample").accept(mediatype.application_json)); actions.andexpect(status().isok()); actions.andexpect(content().string(equalto("hello world! greetings spring boot"))); } } @runwith(springjunit4classrunner.class) @springapplicationconfiguration(classes = bestfirstapplication.class) public abstract class abstractbestfirsttest { protected logger logger = loggerfactory.getlogger(this.getclass()); } @transactional public interface userrepositoryi extends crudrepository<user, long>,userrepositorycustomerizedi { }
here goes pom.xml
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.bestfirst</groupid> <artifactid>bestfirst</artifactid> <version>0.0.1-snapshot</version> <packaging>jar</packaging> <name>bestfirst</name> <description>spring boot project best first</description> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <java.version>1.8</java.version> <retrofit.version>2.0.0-beta3</retrofit.version> <org.springframework.version>4.0.2.release</org.springframework.version> </properties> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.3.1.release</version> <relativepath /> <!-- lookup parent repository --> </parent> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <!-- junit test requires --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <!-- junit test requires --> <!-- jpa --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-jpa</artifactid> </dependency> <!-- jpa --> <!-- http client retrofit --> <dependency> <groupid>com.squareup.retrofit2</groupid> <artifactid>retrofit</artifactid> <version>${retrofit.version}</version> </dependency> <dependency> <groupid>com.squareup.retrofit2</groupid> <artifactid>converter-gson</artifactid> <version>2.0.0-beta3</version> </dependency> <!-- http client retrofit --> <!-- mysql connector --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> </dependency> <!-- mysql connector --> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build>
i problem solved. in spring test senario, must use platformtransactionmanager process transactions. indeed missed part in db config. after added configuration, problem gone!
@configuration @enabletransactionmanagement public class databaseconfig { @value("${db.driver}") private string db_driver; @value("${db.password}") private string db_password; @value("${db.url}") private string db_url; @value("${db.username}") private string db_username; @value("${hibernate.dialect}") private string hibernate_dialect; @value("${hibernate.show_sql}") private string hibernate_show_sql; @value("${hibernate.hbm2ddl.auto}") private string hibernate_hbm2ddl_auto; @value("${entitymanager.packagestoscan}") private string entitymanager_packages_to_scan; @bean public datasource datasource() { drivermanagerdatasource datasource = new drivermanagerdatasource(); datasource.setdriverclassname(db_driver); datasource.seturl(db_url); datasource.setusername(db_username); datasource.setpassword(db_password); return datasource; } @bean public localsessionfactorybean sessionfactory() { localsessionfactorybean sessionfactorybean = new localsessionfactorybean(); sessionfactorybean.setdatasource(datasource()); sessionfactorybean.setpackagestoscan(entitymanager_packages_to_scan); properties hibernateproperties = new properties(); hibernateproperties.put("hibernate.dialect", hibernate_dialect); hibernateproperties.put("hibernate.show_sql", hibernate_show_sql); hibernateproperties.put("hibernate.hbm2ddl.auto", hibernate_hbm2ddl_auto); sessionfactorybean.sethibernateproperties(hibernateproperties); return sessionfactorybean; } @bean public hibernatetransactionmanager transactionmanager() { hibernatetransactionmanager transactionmanager = new hibernatetransactionmanager(); transactionmanager.setsessionfactory(sessionfactory().getobject()); return transactionmanager; } @bean public platformtransactionmanager transactionmanager(entitymanagerfactory emf) { jpatransactionmanager transactionmanager = new jpatransactionmanager(); transactionmanager.setentitymanagerfactory(emf); return transactionmanager; } }
Comments
Post a Comment