Giter Site home page Giter Site logo

stubborn's Introduction

Stubborn

What is this about

TL;DR

Stubborn is a command-line utility that cripples perfect Java classes into shadows of their former selves with stubbed methods. Might be useful for testing.

Current version is 0.1 and development is ongoing.

Longer Version

You know that feeling when you're writing unit tests and it comes down to a part of code which is calling third party library and and you have no way of working around it. And you call that method of that third party class and it has tons of side-effects. Or crashes. Or, maybe, you want to mock it, but it is in final class and you are using Mockito. And you say, "Damn, I want to have this class stripped of all its BS just for testing".

Well, you asked for it.

Stubborn allows you to transform java classes into their test-friendly twins. You can remove finals, you can rewrite methods to do what you need. You can use stubbed classes for testing with greater flexibility.

Development

To start working on Stubborn you will need Java 8 and maven 3. Clone this repository and start hacking.

To start using Stubborn you have following options:

  1. Clone this repository and run mvn package. This will produce jar file with all dependencies.
  2. Use Jar file from bin/ folder
  3. No there are no other options right now

Usage

Stubborn uses Javassist to transform classes and rewrite methods, so you may want to familiarize yourself with Javassist syntax

Stubborn accepts folders and jar files as input and can produce folders and jar files as output. Magic happens in between. Rules by which magic happens are defined in rules file.

Rules file

Rules file is an XML-file which defines how exactly should methods be stubbed.

Simplest rule file could be written as following:

<?xml version="1.0"?>
<rules>
    <methods>
        <method>
            <returntype>java.lang.String</returntype>
            <body>return "";</body>
        </method>
    </methods>
</rules>

This rule will match all methods that return String and will replace their body with a simple return ""; Full syntax of a method matching rule is:

<method>
    <returntype></returntype>
    <classname></classname>
    <methodname></methodname>
    <signature></signature>
    <body></body>
</method>

Supported matchers:

  • ReturnType - matches by canonical name of return type. For Boolean you should use java.lang.Boolean, for primitive types use type name, like boolean
  • ClassName - regular expression which matches class name in which method is defined. Be careful, it matches full class name, so simple ClassName will not work. Use com.example.ClassName or .*ClassName
  • MethodName - another regular expression which matches method name itself
  • Signature - matches method signature, for example (IZ)Ljava/lang/String; will match any method which accepts int and boolean as parameters and returns String

You can skip any matcher. All supplied matchers are combined with AND operation. Obviously it makes no sense in using both ReturnType and Signature.

Put your Java code inside body tag. This code will be compiled and will be used instead original method body. All limitations of Javassist apply.

If method is matched by multiple rules, Stubborn will fail immediately and tell you which matchers are conflicting. You can override this behavior with --ignore-duplicate-matchers parameter, in this case first matcher will be selected.

If method is not matched by any rules its body will be replaced with return null; for all methods which return objects and with return 0; for all methods which return numbers.

If no matching rules are supplied the default one will be used. It is actually the one shown in the example above. All methods which return java.lang.String will return empty string, other methods will be stubbed with default value (i.e. zero, null or false).

There are several more command-line parameters which affect how resulting classes are generated:

  • --strip-non-public will remove all non-public methods and fields from the resulting class
  • --strip-fields will remove all fields from class
  • --strip-final will remove all final modifiers from classes and methods, so you can easily mock them with Mockito or similar tool
  • --help will display more detailed information on command-line parameters

Limitations

Unfortunately, while being written in Java 8 itself, Stubborn does not support class-files generated with Java 8. This is a limitation of Javassist.

Authors

stubborn's People

Contributors

uaraven avatar

Watchers

James Cloos 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.