Git Product home page Git Product logo

common-utils's Introduction

Different Java utility

Build Status

This is a utility for simplifying the writing of tests.

Getting started

To use this tools, you need to add the next repository:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

and dependency:

<dependency>
    <groupId>com.github.antkorwin</groupId>
    <artifactId>common-utils</artifactId>
    <version>0.71</version>
</dependency>

Guard

Guard helps you, when you need to check a some boolean condition and expect a throws Exception if condition is false.

Guard.check( 02 + 010 != 12, CruelJavaWorldException.class, "ooops");

Guard.check( object != null, WrongArgumentException.class, "Object must be not null");

Guard check

You can use this tool while writing a test, to make tests for a checking throw of the exception more readable.

@Test
public void guardWithoutErrorInfo() {

    GuardCheck.check(() -> { throw new IndexOutOfBoundsException(); },
                     IndexOutOfBoundsException.class);
}


@Test
public void testGuardCheck() {

    GuardCheck.check(() -> {
                         throw new NotFoundException(TestErrorInfo.TEST_ERROR);
                     },
                     NotFoundException.class,
                     TestErrorInfo.TEST_ERROR);
}

ErrorInfo

You can use it with an ErrorInfo to accumulate all you domain specific exceptions in an one enum-file:

@ErrorInfoUnique
public enum TestErrorInfo implements ErrorInfo {
    TEST_ERROR("error"),
    TEST_ERROR_ARG("wrong arg");

    private static final int BASE = 1000;
    private final String msg;

    TestErrorInfo(String msg) {
        this.msg = msg;
    }

    @Override
    public String getMessage() {
        return this.msg;
    }

    @Override
    public Integer getCode() {
        return BASE + ordinal();
    }
}

and sample of a test case :

Guard.check(a != b, TestErrorInfo.TEST_ERROR);

Collision Detector for ErrorInfos

In order to ensure the uniqueness of codes in a whole project, you can use enums which implements an ErrorInfo interface.

@Test
public void assertInPackage() {   
    // Act & assert
    ErrorInfoCollisionDetector.assertInPackage("com.demo.project");
}

Utils to work with Garbage Collector

GcUtils

You can use this tool to run the garbage collector and finalize unused memory in your junit tests.

@Test
public void testWeakAfterGC() {
    // Arrange
    String instance = new String("123");
    WeakReference<String> ref = new WeakReference<>(instance);

    // Act
    instance = null;
    GcUtils.fullFinalization();

    // Asserts
    Assertions.assertThat(ref.get()).isNull();
}

Simple memory LeakDetector

You can check a memory leak in your code for a particular object.

@Test
    public void testWithLeak() {
        // Arrange
        Foo foo = new Foo();
        Foo bar = foo;
        LeakDetector leakDetector = new LeakDetector(foo);

        // Act
        foo = null;

        // Asserts
        leakDetector.assertMemoryLeaksExist();
    }

common-utils's People

Contributors

antkorwin avatar

Watchers

 avatar  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.