spring-test-terasoluna-gfw

Logo

Testing Library for Terasoluna Server Framework 5.x with Spring TestContext Framework

View the Project on GitHub yoshikawaa/spring-test-terasoluna-gfw

Library that supports testing of TERASOLUNA Server Framework 5.x Common Library with Spring TestContext Framework.

This is a personal experimental project unrelated to TERASOLUNA. TERASOLUNA is a registered trademark of NTT DATA Corporation.

Notes


Artifacts


spring-test-terasoluna-gfw

Library that easily test function of Terasoluna Framework 5.x Common Libraries.

Getting Start

<dependency>
    <groupId>io.github.yoshikawaa.gfw</groupId>
    <artifactId>spring-test-terasoluna-gfw</artifactId>
    <version>1.1.0</version>
    <scope>test</scope>
</dependency>

Features

Testing support for MockMvc

transaction()

Supports passing token for @TransactionTokenCheck.

import static io.github.yoshikawaa.gfw.test.web.servlet.request.TerasolunaGfwMockMvcRequestPostProcessors.transaction;

    @Test
    public void test() throws Exception {
        mvc.perform(post("/").with(transaction()));
    }

Provides token namespace patterns as follows.

pattern description
transaction() process global token.
(@TransactionTokenCheck)
transaction(String) process single namespace token.
(@TransactionTokenCheck(namespace = "sample"))
transaction(String, String) process combined class and method namespace token.

And you can process invalid token value as transaction().useInvalidToken().

If you send transaction token without transaction(), token will be validated by actual transaction token check mechanism.

transactionToken()

Supports asserting token generated by @TransactionTokenCheck.

import static io.github.yoshikawaa.gfw.test.web.servlet.result.TerasolunaGfwMockMvcResultMatchers.transactionToken;

    @Test
    public void test() throws Exception {
        mvc.perform(post("/").with(transaction()))
            .andExpect(transactionToken().namespace("/"))
            .andExpect(transactionToken().updated());
    }

Provides result matchers as follows.

matcher description
global() assert global token.
(@TransactionTokenCheck)
namespace(String) assert single namespace token.
(@TransactionTokenCheck(namespace = "sample"))
namespace(String, String) assert combined class and method namespace token.
updated() assert token is updated.
(@TransactionTokenCheck(type = TransactionTokenType.IN))
notUpdated() assert token is not updated.
(@TransactionTokenCheck(type = TransactionTokenType.CHECK))
notExists() assert token not exists.

resultMessages()

Supports asserting ResultMessages.

import static io.github.yoshikawaa.gfw.test.web.servlet.result.TerasolunaGfwMockMvcResultMatchers.resultMessages;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.ERROR;

    @Test
    public void test() throws Exception {
        mvc.perform(post("/"))
            .andExpect(resultMessages().type(ERROR))
            .andExpect(resultMessages().messageExists("message1", "message2", "message3"));
    }

Provides result matchers as follows.

matcher description
exists() assert ResultMessages exists.
notExists() assert ResultMessages not exists.
type(ResultMessageType) assert ResultMessages has type.
(ResultMessages.error())
codeExists(String...) assert ResultMessages has codes.
(ResultMessage.fromCode("e.xx.fw.8001"))
textExists(String...) assert ResultMessages has texts.
(ResultMessage.fromText("message"))
messageExists(String...) assert ResultMessages has resolved messages. (required MessageSource)
messageExists(Locale, String...) assert ResultMessages has resolved messages. (required MessageSource)

You can also obtain ResultMessages from various source and name.

resultMessages("messages").fromModel()
source description
fromModel() obtain from model.
(Model#addAttribute())
fromFlashMap() obtain from flash map.
(RedirectAttribute#addFlashAttribute())
fromRequest() obtain from request.
(HttpServletRequest#setAttribute())
fromSession() obtain from session.
(HttpSession#setAttribute())

By default, resultMessages() obtain ResultMessages#DEFAULT_MESSAGES_ATTRIBUTE_NAME attribute from request. This is because SystemExceptionResolver stores ResultMessages in request and flash map with the above name.


spring-test-terasoluna-gfw-context

Library that easily set up tests that based on TERASOLUNA blank project.

Getting Start

<dependency>
    <groupId>io.github.yoshikawaa.gfw</groupId>
    <artifactId>spring-test-terasoluna-gfw-context</artifactId>
    <version>1.1.0</version>
    <scope>test</scope>
</dependency>

Features

Test Context Annotations

@TerasolunaGfwDomainTest

Supports configuring tests for domain layer (@Service, @Repository) that based on TERASOLUNA blank project.

@RunWith(SpringRunner.class) // required only for JUnit4
@TerasolunaGfwDomainTest
public class ServiceTest {
}

Provides features as follows.

@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath*:test-context.xml")

@TerasolunaGfwWebAppTest

Supports configuring tests for web application layer (@Controller) that based on TERASOLUNA blank project.

@RunWith(SpringRunner.class) // required only for JUnit4
@TerasolunaGfwWebAppTest
public class ControllerTest {
}

Provides features as follows.

@ExtendWith(SpringExtension.class)
@WebAppConfiguration
@ContextHierarchy({
        @ContextConfiguration({
            "classpath*:META-INF/spring/applicationContext.xml",
            "classpath*:META-INF/spring/spring-security.xml" }),
        @ContextConfiguration("classpath*:META-INF/spring/spring-mvc.xml") })

MockMvc Builders

TerasolunaGfwMockMvcBuilders

Supports configuring MockMvc that based on TERASOLUNA blank project.

@RunWith(SpringRunner.class) // required only for JUnit4
@TerasolunaGfwWebAppTest
public class ControllerTest {
  @AutoWired
  WebApplicationContext context;
  MockMvc mvc;

  @Before
  public void setup() {
    mvc = TerasolunaGfwMockMvcBuilders.setup(context)
            .alwaysDo(log())
            .build();
  }

  @Test
  public void test() throws Exception {
    mvc.perform(get("/test")...
  }
}

Provides features as follows.