Skip to main content

Choosing a Test Type

You can test an NS applicationNS Application (NSApplication). A pre-built software stack that provides common enterprise application functionality such as CRUD, search, authentication, and business process modeling out of the box. on three levels. Each level tests more of the applicationApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution., but is also slower and harder to set up.

Test typeWhat runsSpeed
Unit testOne class, without the framework. Its dependencies are mocked.Milliseconds
Test framework testLogic layerLayerLogical separation of functionality within a single system/application, focused on code organization., data layerLayerLogical separation of functionality within a single system/application, focused on code organization. and queries on an in-memory database. Beans can be mocked.Seconds
Integration testThe deployed applicationApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution., called through its API.Slow. Needs a running applicationApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution. and database.

Write each test on the lowest level that can catch the bug you want to prevent.

Decision tree​

The test needs the deployed applicationApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution. when it depends on the REST API, authentication, the applicationApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution. server configuration or external systemsSystemA set of interconnected components that work together to achieve a specific purpose or function..

Typical cases:

You want to test...Test type
A calculation, conversion or validation in a helper classUnit test
A custom finderFinderA search specification combining field-operator pairs to execute standard data queries on a DataElement., native query or query searchQuery Search (QuerySearch). An advanced search mechanism using declarative constraints to build complex, multi-variable or cross-component queries.Test framework test without mocks
The validations and side effects of a create, modify or deleteTest framework test without mocks
How a bean reacts when another bean returns an errorTest framework test with mocked beans
That an API endpoint returns the right responseIntegration test
A flow through several applicationsApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution. or external systemsSystemA set of interconnected components that work together to achieve a specific purpose or function.Integration test

Unit test​

A unit test checks one class without the framework. Use it for code that does not need agents, beans or the database. Unit tests are the fastest tests, so prefer them whenever the code allows it.

The example tests a helper class that formats a numberplate.

public class NumberplateFormatterTest {

@Test
public void testFormatAddsDashes() {
NumberplateFormatter formatter = new NumberplateFormatter();

String numberplate = formatter.format("1abc123");

assertEquals("1-ABC-123", numberplate);
}
}

Once a unit test needs several mocks to set up, consider a test framework test instead.

Test framework test​

A test framework test runs the logic and data layerLayerLogical separation of functionality within a single system/application, focused on code organization. without an applicationApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution. server. See Test Framework for the setup.

Without mocks​

Without mocks, the test runs the real beans, validations and queries against the in-memory database. Use this for findersFinderA search specification combining field-operator pairs to execute standard data queries on a DataElement., query searchesQuery Search (QuerySearch). An advanced search mechanism using declarative constraints to build complex, multi-variable or cross-component queries., CRUD validations and other logic that depends on the data.

The example creates two cars and checks that the finderFinderA search specification combining field-operator pairs to execute standard data queries on a DataElement. findByBrandEq only returns the car of the given brand.

@WithTestFramework
public class CarFinderTest {

@Test
public void testFindByBrandEq() {
Context context = Context.emptyContext();
CarDetails mercedes = mercedesCla().createAndGetDetails(context);
volkswagenGolf().create(context);

CarFindByBrandEqDetails finderDetails = new CarFindByBrandEqDetails();
finderDetails.setBrand("Mercedes");

CarLocalAgent carAgent = CarLocalAgent.getCarAgent(context);
SearchResult<CarDetails> searchResult = carAgent.find(SearchDetails.fetchAllDetails(finderDetails));

assertTrue(searchResult.isSuccess());
assertEquals(1, searchResult.getTotalNumberOfItems());
assertEquals(mercedes.getId(), searchResult.getFirstResult().get().getId());
}
}

mercedesCla() and volkswagenGolf() come from a test data factory.

With mocked beans​

Mock a bean when its real behaviour is not relevant for the test, or when you want to force a specific result such as an error. If the test never reaches the database, the in-memory database does not start and the test runs faster.

The example checks how the applicationApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution. handles a failing query searchQuery Search (QuerySearch). An advanced search mechanism using declarative constraints to build complex, multi-variable or cross-component queries., without creating any data.

@WithTestFramework
public class CarQuerySearchErrorTest {

@BeforeAll
public static void setup() {
class QuerySearchMock extends CarBean {
@Override
public <S extends CarElementQueryFilter, T> SearchResult<T> querySearch(ParameterContext<SearchDetails<S>> searchParameter) {
return SearchResult.error(Diagnostic.error("carRental", "Car", "querysearch.failed"));
}
}
TestBeanRegistry.LOCAL_REGISTRY.registerImplementation(CarLocal.class, QuerySearchMock.class);
}

@Test
public void testQuerySearchError() {
CarLocalAgent carAgent = CarLocalAgent.getCarAgent(Context.emptyContext());

SearchResult<CarDetails> searchResult = carAgent.querySearch(SearchDetails.fetchAllDetails(new CarQueryFilter()));

assertTrue(searchResult.isError());
assertEquals("querysearch.failed", searchResult.getDiagnostics().get(0).getKey());
}
}

See Mocking beans for all ways to register mocks.

Integration test​

An integration test calls the deployed applicationApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution. through its API, in the same way a front-end or external systemSystemA set of interconnected components that work together to achieve a specific purpose or function. does. Use it to test what the test framework cannot simulate: the API layerLayerLogical separation of functionality within a single system/application, focused on code organization., authentication and authorization, applicationApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution. server configuration and external systemsSystemA set of interconnected components that work together to achieve a specific purpose or function..

Integration tests need a running applicationApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution. and database. They are slow and harder to keep stable. Keep them for the main flows and test the details with the test framework.

Unlike test framework tests, integration tests are not rolled back. Clean up the data you create, or run the tests against a database that is reset before each run.

For applicationsApplicationThe overarching model container grouping all components, domain logic, and configurations for an enterprise solution. with the Service API, the rest-expandersExpanderA unit of generation that combines a data tree instance (functional M1 model) with a template to generate an artifact. generate an integration test project with basic CRUD tests for every endpoint. See Integration Testing.