Skip to content

Commit

Permalink
added complete integration test with stubbing db-access
Browse files Browse the repository at this point in the history
  • Loading branch information
feech committed Apr 25, 2019
1 parent 379aa69 commit 1724f93
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
11 changes: 11 additions & 0 deletions complete/pom.xml
Expand Up @@ -26,6 +26,17 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

<dependency>

This comment has been minimized.

Copy link
@feech

feech Apr 26, 2019

Author

add libraries

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.anystub</groupId>
<artifactId>anystub</artifactId>
<version>0.2.22</version>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down
21 changes: 21 additions & 0 deletions complete/src/test/java/hello/ApplicationTest.java
@@ -0,0 +1,21 @@
package hello;

import org.anystub.AnyStubId;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.*;

@RunWith(SpringRunner.class)

This comment has been minimized.

Copy link
@feech

feech Apr 26, 2019

Author

add empty test to run full application context in the test

@SpringBootTest
public class ApplicationTest {

@Test
@AnyStubId
public void test() {

}

}
22 changes: 22 additions & 0 deletions complete/src/test/java/hello/TestConfiguration.java
@@ -0,0 +1,22 @@
package hello;


import org.anystub.jdbc.StubDataSource;
import org.h2.jdbcx.JdbcDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class TestConfiguration {

This comment has been minimized.

Copy link
@feech

feech Apr 26, 2019

Author

you can use the single configuration for all tests
to split all interactions in different stub files you have two options:

  • @AnyStubId - which is applicable to test classes or test methods, to specify a name of a stub-file for all tests in a class or individual test method
  • .setStubSuffix method of StubDataSource class - to specify a suffix for all stub-files related to specific connection. it's useful if your application holds connections to several databases at a time.

@Bean
public DataSource dataSource() {

JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:./test");

This comment has been minimized.

Copy link
@feech

feech Apr 26, 2019

Author

create datasource for real database
and wrap it with stubbing datasource

return new StubDataSource(ds);

This comment was marked as resolved.

Copy link
@feech

feech Apr 26, 2019

Author

wrap real datasource

}

}
68 changes: 68 additions & 0 deletions complete/src/test/resources/anystub/stub.yml
@@ -0,0 +1,68 @@
request0:

This comment has been minimized.

Copy link
@feech

feech Apr 26, 2019

Author

the stub file is created after the first run of the test,
Usually you need to add the stub-file in your repository.

The first run of the test creates test.mv.db in root of your project as a real communication required.
Later having the stub file prevents your application request the real database. you can remove the test.mv.db file and run test again. application will get all data from the stub.

You can even edit the stub-file if you wish your application receives other data

If you believe the data changed in DB and you need to update your tests, you can always remove the stub and re-run your tests. the new stub-file will be created.

exception: []
keys: DROP TABLE customers IF EXISTS
values: 'false'
request1:
exception: []
keys: CREATE TABLE customers(id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))
values: 'false'
request2:
exception: []
keys: DatabaseMetaData:supportsBatchUpdates
values: 'true'
request3:
exception: []
keys: ['INSERT INTO customers(first_name, last_name) VALUES (?,?)', '1', John, '2',
Woo, '1', Jeff, '2', Dean, '1', Josh, '2', Bloch, '1', Josh, '2', Long]
values: ['1', '1', '1', '1']
request4:
exception: []
keys: ['SELECT id, first_name, last_name FROM customers WHERE first_name = ?', '1',
Josh]
values: ['3', ID, INTEGER, '4', '10', '0', FIRST_NAME, VARCHAR, '12', '255', '0',
LAST_NAME, VARCHAR, '12', '255', '0']
request5:
exception: []
keys: [resultSet, 'SELECT id, first_name, last_name FROM customers WHERE first_name
= ?', next]
values: 'true'
request6:
exception: []
keys: [resultSet, 'SELECT id, first_name, last_name FROM customers WHERE first_name
= ?', getLong, id]
values: '3'
request7:
exception: []
keys: [resultSet, 'SELECT id, first_name, last_name FROM customers WHERE first_name
= ?', getString, first_name]
values: Josh
request8:
exception: []
keys: [resultSet, 'SELECT id, first_name, last_name FROM customers WHERE first_name
= ?', getString, last_name]
values: Bloch
request9:
exception: []
keys: [resultSet, 'SELECT id, first_name, last_name FROM customers WHERE first_name
= ?', next, '#1']
values: 'true'
request10:
exception: []
keys: [resultSet, 'SELECT id, first_name, last_name FROM customers WHERE first_name
= ?', getLong, id, '#1']
values: '4'
request11:
exception: []
keys: [resultSet, 'SELECT id, first_name, last_name FROM customers WHERE first_name
= ?', getString, first_name, '#1']
values: Josh
request12:
exception: []
keys: [resultSet, 'SELECT id, first_name, last_name FROM customers WHERE first_name
= ?', getString, last_name, '#1']
values: Long
request13:
exception: []
keys: [resultSet, 'SELECT id, first_name, last_name FROM customers WHERE first_name
= ?', next, '#2']
values: 'false'

1 comment on commit 1724f93

@feech
Copy link
Author

@feech feech commented on 1724f93 Apr 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually the example doesn't test anything, It only creates a stub for run() method in main class.
it's because of the application doesn't include any of class to test.
you can check some examples here jdbc-IT test to see how it could work.

Please sign in to comment.