GithubHelp home page GithubHelp logo

isabella232 / web3j-unit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from web3j/web3j-unit

0.0 0.0 0.0 1.95 MB

Smart contract testing framework via integrated EVM and various Ethereum clients

Home Page: https://www.web3labs.com/epirus

License: Apache License 2.0

Shell 8.22% Kotlin 71.02% Java 18.80% Solidity 1.96%

web3j-unit's Introduction

Web3j-unit Build Status

Web3j-unit is a Junit 5 extension to streamline the creation of Ethereum contract tests.

Multiple Ethereum implementations are supported including Geth and Besu. To run tests built using Web3j-unit, docker is required on the host.

Instances of Web3j, TransactionManager and GasProvider are injected into the Junit runner.

You can find a sample here.

You can find an example using docker-compose here. This spins up VMWare Concord nodes using a docker-compose file.

Getting Started

  1. Add dependency to gradle.
   repositories {
      mavenCentral()
      jcenter()
   }

   implementation "org.web3j:core:4.8.9"
   testCompile "org.web3j:web3j-unit:4.8.9"
  1. Create a new test with the @EVMTest annotation. An embedded EVM is used by default. To use Geth or Besu pass the node type into the annotation: @EVMTest(NodeType.GETH) or @EVMTest(NodeType.BESU)
@EVMTest
class GreeterTest {

}
  1. Inject instance of Web3j TransactionManager and ContractGasProvider in your test method.
@EVMTest
class GreeterTest {

    @Test
    fun greeterDeploys(
        web3j: Web3j,
        transactionManager: TransactionManager,
        gasProvider: ContractGasProvider
    ) {}

}
  1. Deploy your contract in the test.
@EVMTest
class GreeterTest {

    @Test
    fun greeterDeploys(
        web3j: Web3j,
        transactionManager: TransactionManager,
        gasProvider: ContractGasProvider
    ) {
        val greeter = Greeter.deploy(web3j, transactionManager, gasProvider, "Hello EVM").send()
        val greeting = greeter.greet().send()
        assertEquals("Hello EVM", greeting)
    }

}
  1. Run the test!

Using a custom docker-compose file

  1. Add dependency to gradle.
  repositories {
     mavenCentral()
     jcenter()
  }

  implementation "org.web3j:core:4.8.8"
  testCompile "org.web3j:web3j-unit:4.8.8"
  1. Create a new test with the @EVMComposeTest annotation. By default, uses test.yml file in the project home, and runs web3j on service name node1 exposing the port 8545. Can be customised to use specific docker-compose file, service name and port by @EVMComposeTest("src/test/resources/geth.yml", "ethnode1", 8080) Here, we connect to the service named ethnode1 in the src/test/resources/geth.yml docker-compose file which exposes the port 8080 for web3j to connect to.
@EVMComposeTest("src/test/resources/geth.yml", "ethnode1", 8080)
class GreeterTest {

}
  1. Inject instance of Web3j TransactionManager and ContractGasProvider in your test method.
@EVMComposeTest("src/test/resources/geth.yml", "ethnode1", 8080)
class GreeterTest {

    @Test
    fun greeterDeploys(
        web3j: Web3j,
        transactionManager: TransactionManager,
        gasProvider: ContractGasProvider
    ) {}

}
  1. Deploy your contract in the test.
@EVMComposeTest("src/test/resources/geth.yml", "ethnode1", 8080)
class GreeterTest {

    @Test
    fun greeterDeploys(
        web3j: Web3j,
        transactionManager: TransactionManager,
        gasProvider: ContractGasProvider
    ) {
        val greeter = Greeter.deploy(web3j, transactionManager, gasProvider, "Hello EVM").send()
        val greeting = greeter.greet().send()
        assertEquals("Hello EVM", greeting)
    }

}
  1. Run the test!

web3j-unit's People

Contributors

alexandrour avatar antonydenyer avatar cfelde avatar conor10 avatar iikirilov avatar josh-richardson avatar puneetha17 avatar rach-id avatar xaviarias avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.