顯示具有 JUnit 標籤的文章。 顯示所有文章
顯示具有 JUnit 標籤的文章。 顯示所有文章

2018年8月2日 星期四

[Java] test Jersey injection

參考 StackOverflow:

// src/test/java/example/HellpResourceTest.java
package example;

import javax.inject.Inject;
import javax.ws.rs.core.Response;

import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.utilities.Binder;
import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import example.HelloResource;

class HelloResourceTest {
    @Inject
    private HelloResource resource;
    private ServiceLocator locator;

    @BeforeEach
    void setUp() {
        Binder resourceBinder = new AbstractBinder() {
            @Override
            public void configure() {
                // bind resource
                bindAsContract(HelloResource.class);
                // bind mock repository
                bind(new MockHelloRepository()).to(HelloRepository.class);
            }
        };
        locator = ServiceLocatorUtilities.bind(repositoryBinder, resourceBinder);
        locator.inject(this);
    }

    @AfterEach
    void tearDown() {
        if (locator != null) {
            locator.shutdown();
        }
    }

    @Test
    @DisplayName("Test Hello API")
    void doTest() {
        Assertions.assertNotNull(resource);
        Response response = resource.getHello("World");
        System.out.println(response);
        Assertions.assertEquals("Hello World", response);
    }

}

用到 org.glassfish.hk2:hk2-locator:2.5.0, org.glassfish.hk2:hk2-utils:2.5.0

參考資料:

[Java] JUnit with DynamoDBLocal

先設定 build.gradle:

// build.gradle
repositories {
    maven {
        // dynamodb local url
        url 'https://s3-us-west-2.amazonaws.com/dynamodb-local/release'
    }

    testCompile (
        'org.junit.jupiter:junit-jupiter-api:5.2.0',
        'com.amazonaws:DynamoDBLocal:1.11.119'
    )

    testRuntime (
        // junit5
        'org.junit.jupiter:junit-jupiter-engine:5.2.0',
        // dynamodb local
        'com.almworks.sqlite4java:libsqlite4java-linux-i386:1.0.392',
        'com.almworks.sqlite4java:libsqlite4java-osx:1.0.392'
    )
}
// link native sqlite4java
task copyNativeDeps(type: Copy) {
    from (configurations.testCompile) {
        include '*.dylib'
        include '*.so'
        include '*.dll'
    }
    into 'build/libs'
}

test {
    useJUnitPlatform()
    dependsOn copyNativeDeps
    doFirst {
        // setup sqlite4java path
        systemProperty 'java.library.path', 'build/libs'
    }
    testLogging {
        showStandardStreams = true
    }
}

接下來就是測試:

package example;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Test;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.local.embedded.DynamoDBEmbedded;
import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;

public class DynamoDBLocalTest {

    @Test
    public void createTable() throws Exception {
        AmazonDynamoDB client = DynamoDBEmbedded.create().amazonDynamoDB();
        DynamoDB dynamodb = new DynamoDB(client);
        String tableName = "Employee";

        List keySchema = new ArrayList();
        keySchema.add(new KeySchemaElement().withAttributeName("Name").withKeyType(KeyType.HASH));

        List attributeDefinitions = new ArrayList();
        attributeDefinitions.add(new AttributeDefinition().withAttributeName("Name").withAttributeType("S"));

        CreateTableRequest request = new CreateTableRequest().withTableName(tableName).withKeySchema(keySchema)
                .withProvisionedThroughput(
                        new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L))
                .withAttributeDefinitions(attributeDefinitions);

        System.out.println("Issuing CreateTable request for " + tableName);
        Table table = dynamodb.createTable(request);
        System.out.println("Waiting for " + tableName + " to be created...this may take a while...");
        table.waitForActive();

        System.out.println(dynamodb.getTable(tableName).describe());
    }

}

參考資料:

系列:

[Java] Invalid HTTP method: PATCH

最近系統需要使用 Netty4,所以把衝突的 Netty3 拆掉,然後就出現了例外。 pom.xml <dependency> <groupId>com.ning</groupId> <artifactId>as...