Skip to content

Use h2 for Unit Test #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
*#
*.iml
*.ipr
*.iws
*.jar
*.sw?
*~
.#*
.*.md.html
.DS_Store
.attach_pid*
.classpath
.factorypath
.gradle
.idea
.metadata
.project
.recommenders
.settings
.springBeans
.vscode
/code
MANIFEST.MF
_site/
activemq-data
bin
build
!/**/src/**/bin
!/**/src/**/build
build.log
dependency-reduced-pom.xml
dump.rdb
interpolated*.xml
lib/
manifest.yml
out
overridedb.*
target
.flattened-pom.xml
secrets.yml
.gradletasknamecache
.sts4-cache
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<java.version>11</java.version>
</properties>

<build>
<plugi<properties>
<java.version>11</java.version>
</properties>ns>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface StudentRepository extends JpaRepository<Student, Long> {

public List<Student> findByFirstNameContaining(String firstName);

@Query(value = " select * from schooldb.mycustom_table where student_id= :emailId", nativeQuery = true)
@Query(value = " select * from mycustom_table where student_id= :emailId", nativeQuery = true)
Student findByIdQueryMethod(@Param("emailId") Long i);


Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/schooldb
spring.datasource.username=root
spring.datasource.password=root@123
spring.datasource.driver-class-name =com.mysql.jdbc.Driver
spring.jpa.show-sql: true
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.ninjaarun.my.spring.jpa.tutorial;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class MySpringJpaTutorialApplicationTests {

@Test
void contextLoads() {
public void main() {
MySpringJpaTutorialApplication.main(new String[]{});
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
import com.ninjaarun.my.spring.jpa.tutorial.entity.Course;
import com.ninjaarun.my.spring.jpa.tutorial.entity.CourseMaterial;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@SpringBootTest
import static org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace.NONE;

@ExtendWith(SpringExtension.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = NONE)
class CourseMaterialRepositoryTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
import com.ninjaarun.my.spring.jpa.tutorial.entity.Guardian;
import com.ninjaarun.my.spring.jpa.tutorial.entity.Student;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.List;

@SpringBootTest
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace.NONE;

@ExtendWith(SpringExtension.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = NONE)
class StudentRepositoryTest {

@Autowired
Expand Down Expand Up @@ -52,17 +62,21 @@ public void saveStudentWithGuardian()
.build();

studentRepository.save(s);

List<Student> results = studentRepository.findByFirstNameContaining("Neetu");
assertFalse(results.isEmpty());
}

@Test
public void findByFirstName()
{
System.out.println(studentRepository.findByFirstNameContaining("arun"));
assertTrue(studentRepository.findByFirstNameContaining("arun").isEmpty());
}

@Test
public void findByIdQueryMethod()
{
System.out.println(studentRepository.findByIdQueryMethod(2L));
Student result = studentRepository.findByIdQueryMethod(2L);
assertNull(result);
}
}
7 changes: 7 additions & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spring.h2.console.enabled=true

spring.datasource.platform=h2
spring.datasource.initialization-mode=embedded
spring.datasource.url=jdbc:h2:mem:memonly;MODE=PostgreSQL;IGNORECASE=TRUE;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=
6 changes: 0 additions & 6 deletions target/classes/application.properties

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.