Spring Boot Unit Test - Unable to get my tests working












-1














I'm working on a Spring Boot project, and need to write Unit Tests for my Repository and service layers. However, I'm unable to get the ball rolling as none of my unit tests work. i always get some kind of configuration errors.



This is my test class:



@RunWith(SpringRunner.class)
@DataJpaTest
public class PersonRepositoryIntegrationTest {

@Autowired
private TestEntityManager testEntityManager;

@Autowired
private PersonRepository personRepository;

private void createPerson() {
Person p = new Person();
p.setName("Sriram");
p.setEmail("sriram@xxxyyy.in");
this.testEntityManager.persist(p);
this.testEntityManager.flush();

Person p2 = new Person();
p2.setName("John Doe");
p2.setEmail("john.doe@xxxyyy.in");
this.testEntityManager.persist(p2);
this.testEntityManager.flush();
}

@Test
public void testMethod() {
this.createPerson();

Person found = personRepository.findByEmail("sriram@xxxyyy.in");

assertThat(found.getName()).isEqualTo("Sriram");
}
}


When I run this, I get the following error:



java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.h2.jdbc.JdbcSQLException: Table "ALL_SEQUENCES" not found; SQL statement:
select sequence_name from all_sequences union select synonym_name from all_synonyms us, all_sequences asq where asq.sequence_name = us.table_name and asq.sequence_owner = us.table_owner [42102-197]


This is just one of the many config errors I've been getting for the past day. The error above was when I was following this tutorial step by step. Doesn't seem to work.



This is my POM



<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.1.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


As you may have noted I'm using Oracle DB actually, but starting a H2 DB for test purposes.



Can anyone help me out why I'm getting the error? Should I have a separate application.properties with DB configuration for my test classes?



I can provide more information if you need, but I need to crack this.



Appreciate any help I can get. Thanks!



**EDIT: ** application.properties added as requested by Anand



hibernate.dialect = org.hibernate.dialect.Oracle10gDialect
hibernate.hbm2ddl.auto=create
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
jdbc.url=jdbc:oracle:thin:@localhost:1521:xe
jdbc.username=tm
jdbc.password=tm
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory









share|improve this question
























  • post your application.property file for verification and configs if you have any
    – Anand Varkey Philips
    Nov 23 at 6:16












  • Well your question states the problem > * I'm using Oracle DB actually, but starting a H2 DB for test purposes.*... This is your problem. H2 isn't Oracle and the query being issued is an oracle one. Configure hibernate for the correct database you want to use. For testing that is H2 and not oracle.
    – M. Deinum
    Nov 23 at 8:15










  • @M.Deinum I guessed that, but the tutorial I read (link in my post) stated that I could actually use H2 only for testing purposes rather than bring up my actual database. That's the reason I did not put too much thought into it. That said, how do I configure H2 only for testing? Could you please elaborate?
    – sriramsridharan
    Nov 23 at 10:39










  • @AnandVarkeyPhilips Added the properties file. Please check
    – sriramsridharan
    Nov 23 at 10:45










  • You can use H2 for testing but it will not understand Oracle SQL query. So you need to tell JPA that you are using H2 instead of Oracle else it will generate the wrong queries.
    – M. Deinum
    Nov 23 at 12:06
















-1














I'm working on a Spring Boot project, and need to write Unit Tests for my Repository and service layers. However, I'm unable to get the ball rolling as none of my unit tests work. i always get some kind of configuration errors.



This is my test class:



@RunWith(SpringRunner.class)
@DataJpaTest
public class PersonRepositoryIntegrationTest {

@Autowired
private TestEntityManager testEntityManager;

@Autowired
private PersonRepository personRepository;

private void createPerson() {
Person p = new Person();
p.setName("Sriram");
p.setEmail("sriram@xxxyyy.in");
this.testEntityManager.persist(p);
this.testEntityManager.flush();

Person p2 = new Person();
p2.setName("John Doe");
p2.setEmail("john.doe@xxxyyy.in");
this.testEntityManager.persist(p2);
this.testEntityManager.flush();
}

@Test
public void testMethod() {
this.createPerson();

Person found = personRepository.findByEmail("sriram@xxxyyy.in");

assertThat(found.getName()).isEqualTo("Sriram");
}
}


When I run this, I get the following error:



java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.h2.jdbc.JdbcSQLException: Table "ALL_SEQUENCES" not found; SQL statement:
select sequence_name from all_sequences union select synonym_name from all_synonyms us, all_sequences asq where asq.sequence_name = us.table_name and asq.sequence_owner = us.table_owner [42102-197]


This is just one of the many config errors I've been getting for the past day. The error above was when I was following this tutorial step by step. Doesn't seem to work.



This is my POM



<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.1.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


As you may have noted I'm using Oracle DB actually, but starting a H2 DB for test purposes.



Can anyone help me out why I'm getting the error? Should I have a separate application.properties with DB configuration for my test classes?



I can provide more information if you need, but I need to crack this.



Appreciate any help I can get. Thanks!



**EDIT: ** application.properties added as requested by Anand



hibernate.dialect = org.hibernate.dialect.Oracle10gDialect
hibernate.hbm2ddl.auto=create
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
jdbc.url=jdbc:oracle:thin:@localhost:1521:xe
jdbc.username=tm
jdbc.password=tm
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory









share|improve this question
























  • post your application.property file for verification and configs if you have any
    – Anand Varkey Philips
    Nov 23 at 6:16












  • Well your question states the problem > * I'm using Oracle DB actually, but starting a H2 DB for test purposes.*... This is your problem. H2 isn't Oracle and the query being issued is an oracle one. Configure hibernate for the correct database you want to use. For testing that is H2 and not oracle.
    – M. Deinum
    Nov 23 at 8:15










  • @M.Deinum I guessed that, but the tutorial I read (link in my post) stated that I could actually use H2 only for testing purposes rather than bring up my actual database. That's the reason I did not put too much thought into it. That said, how do I configure H2 only for testing? Could you please elaborate?
    – sriramsridharan
    Nov 23 at 10:39










  • @AnandVarkeyPhilips Added the properties file. Please check
    – sriramsridharan
    Nov 23 at 10:45










  • You can use H2 for testing but it will not understand Oracle SQL query. So you need to tell JPA that you are using H2 instead of Oracle else it will generate the wrong queries.
    – M. Deinum
    Nov 23 at 12:06














-1












-1








-1







I'm working on a Spring Boot project, and need to write Unit Tests for my Repository and service layers. However, I'm unable to get the ball rolling as none of my unit tests work. i always get some kind of configuration errors.



This is my test class:



@RunWith(SpringRunner.class)
@DataJpaTest
public class PersonRepositoryIntegrationTest {

@Autowired
private TestEntityManager testEntityManager;

@Autowired
private PersonRepository personRepository;

private void createPerson() {
Person p = new Person();
p.setName("Sriram");
p.setEmail("sriram@xxxyyy.in");
this.testEntityManager.persist(p);
this.testEntityManager.flush();

Person p2 = new Person();
p2.setName("John Doe");
p2.setEmail("john.doe@xxxyyy.in");
this.testEntityManager.persist(p2);
this.testEntityManager.flush();
}

@Test
public void testMethod() {
this.createPerson();

Person found = personRepository.findByEmail("sriram@xxxyyy.in");

assertThat(found.getName()).isEqualTo("Sriram");
}
}


When I run this, I get the following error:



java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.h2.jdbc.JdbcSQLException: Table "ALL_SEQUENCES" not found; SQL statement:
select sequence_name from all_sequences union select synonym_name from all_synonyms us, all_sequences asq where asq.sequence_name = us.table_name and asq.sequence_owner = us.table_owner [42102-197]


This is just one of the many config errors I've been getting for the past day. The error above was when I was following this tutorial step by step. Doesn't seem to work.



This is my POM



<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.1.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


As you may have noted I'm using Oracle DB actually, but starting a H2 DB for test purposes.



Can anyone help me out why I'm getting the error? Should I have a separate application.properties with DB configuration for my test classes?



I can provide more information if you need, but I need to crack this.



Appreciate any help I can get. Thanks!



**EDIT: ** application.properties added as requested by Anand



hibernate.dialect = org.hibernate.dialect.Oracle10gDialect
hibernate.hbm2ddl.auto=create
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
jdbc.url=jdbc:oracle:thin:@localhost:1521:xe
jdbc.username=tm
jdbc.password=tm
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory









share|improve this question















I'm working on a Spring Boot project, and need to write Unit Tests for my Repository and service layers. However, I'm unable to get the ball rolling as none of my unit tests work. i always get some kind of configuration errors.



This is my test class:



@RunWith(SpringRunner.class)
@DataJpaTest
public class PersonRepositoryIntegrationTest {

@Autowired
private TestEntityManager testEntityManager;

@Autowired
private PersonRepository personRepository;

private void createPerson() {
Person p = new Person();
p.setName("Sriram");
p.setEmail("sriram@xxxyyy.in");
this.testEntityManager.persist(p);
this.testEntityManager.flush();

Person p2 = new Person();
p2.setName("John Doe");
p2.setEmail("john.doe@xxxyyy.in");
this.testEntityManager.persist(p2);
this.testEntityManager.flush();
}

@Test
public void testMethod() {
this.createPerson();

Person found = personRepository.findByEmail("sriram@xxxyyy.in");

assertThat(found.getName()).isEqualTo("Sriram");
}
}


When I run this, I get the following error:



java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.h2.jdbc.JdbcSQLException: Table "ALL_SEQUENCES" not found; SQL statement:
select sequence_name from all_sequences union select synonym_name from all_synonyms us, all_sequences asq where asq.sequence_name = us.table_name and asq.sequence_owner = us.table_owner [42102-197]


This is just one of the many config errors I've been getting for the past day. The error above was when I was following this tutorial step by step. Doesn't seem to work.



This is my POM



<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.1.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


As you may have noted I'm using Oracle DB actually, but starting a H2 DB for test purposes.



Can anyone help me out why I'm getting the error? Should I have a separate application.properties with DB configuration for my test classes?



I can provide more information if you need, but I need to crack this.



Appreciate any help I can get. Thanks!



**EDIT: ** application.properties added as requested by Anand



hibernate.dialect = org.hibernate.dialect.Oracle10gDialect
hibernate.hbm2ddl.auto=create
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
jdbc.url=jdbc:oracle:thin:@localhost:1521:xe
jdbc.username=tm
jdbc.password=tm
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory






spring-boot junit






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 10:44

























asked Nov 23 at 4:48









sriramsridharan

180417




180417












  • post your application.property file for verification and configs if you have any
    – Anand Varkey Philips
    Nov 23 at 6:16












  • Well your question states the problem > * I'm using Oracle DB actually, but starting a H2 DB for test purposes.*... This is your problem. H2 isn't Oracle and the query being issued is an oracle one. Configure hibernate for the correct database you want to use. For testing that is H2 and not oracle.
    – M. Deinum
    Nov 23 at 8:15










  • @M.Deinum I guessed that, but the tutorial I read (link in my post) stated that I could actually use H2 only for testing purposes rather than bring up my actual database. That's the reason I did not put too much thought into it. That said, how do I configure H2 only for testing? Could you please elaborate?
    – sriramsridharan
    Nov 23 at 10:39










  • @AnandVarkeyPhilips Added the properties file. Please check
    – sriramsridharan
    Nov 23 at 10:45










  • You can use H2 for testing but it will not understand Oracle SQL query. So you need to tell JPA that you are using H2 instead of Oracle else it will generate the wrong queries.
    – M. Deinum
    Nov 23 at 12:06


















  • post your application.property file for verification and configs if you have any
    – Anand Varkey Philips
    Nov 23 at 6:16












  • Well your question states the problem > * I'm using Oracle DB actually, but starting a H2 DB for test purposes.*... This is your problem. H2 isn't Oracle and the query being issued is an oracle one. Configure hibernate for the correct database you want to use. For testing that is H2 and not oracle.
    – M. Deinum
    Nov 23 at 8:15










  • @M.Deinum I guessed that, but the tutorial I read (link in my post) stated that I could actually use H2 only for testing purposes rather than bring up my actual database. That's the reason I did not put too much thought into it. That said, how do I configure H2 only for testing? Could you please elaborate?
    – sriramsridharan
    Nov 23 at 10:39










  • @AnandVarkeyPhilips Added the properties file. Please check
    – sriramsridharan
    Nov 23 at 10:45










  • You can use H2 for testing but it will not understand Oracle SQL query. So you need to tell JPA that you are using H2 instead of Oracle else it will generate the wrong queries.
    – M. Deinum
    Nov 23 at 12:06
















post your application.property file for verification and configs if you have any
– Anand Varkey Philips
Nov 23 at 6:16






post your application.property file for verification and configs if you have any
– Anand Varkey Philips
Nov 23 at 6:16














Well your question states the problem > * I'm using Oracle DB actually, but starting a H2 DB for test purposes.*... This is your problem. H2 isn't Oracle and the query being issued is an oracle one. Configure hibernate for the correct database you want to use. For testing that is H2 and not oracle.
– M. Deinum
Nov 23 at 8:15




Well your question states the problem > * I'm using Oracle DB actually, but starting a H2 DB for test purposes.*... This is your problem. H2 isn't Oracle and the query being issued is an oracle one. Configure hibernate for the correct database you want to use. For testing that is H2 and not oracle.
– M. Deinum
Nov 23 at 8:15












@M.Deinum I guessed that, but the tutorial I read (link in my post) stated that I could actually use H2 only for testing purposes rather than bring up my actual database. That's the reason I did not put too much thought into it. That said, how do I configure H2 only for testing? Could you please elaborate?
– sriramsridharan
Nov 23 at 10:39




@M.Deinum I guessed that, but the tutorial I read (link in my post) stated that I could actually use H2 only for testing purposes rather than bring up my actual database. That's the reason I did not put too much thought into it. That said, how do I configure H2 only for testing? Could you please elaborate?
– sriramsridharan
Nov 23 at 10:39












@AnandVarkeyPhilips Added the properties file. Please check
– sriramsridharan
Nov 23 at 10:45




@AnandVarkeyPhilips Added the properties file. Please check
– sriramsridharan
Nov 23 at 10:45












You can use H2 for testing but it will not understand Oracle SQL query. So you need to tell JPA that you are using H2 instead of Oracle else it will generate the wrong queries.
– M. Deinum
Nov 23 at 12:06




You can use H2 for testing but it will not understand Oracle SQL query. So you need to tell JPA that you are using H2 instead of Oracle else it will generate the wrong queries.
– M. Deinum
Nov 23 at 12:06

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440787%2fspring-boot-unit-test-unable-to-get-my-tests-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440787%2fspring-boot-unit-test-unable-to-get-my-tests-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks