Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


Build Status Coverage Status License Maven Central

Java framework for a usecase centric architecture as proposed by Robert C. Martin and others. Supports Java 1.6+.


JUsecase is available on maven central repository:


Don't forget to add one of jusecase-test-junit4 or jusecase-test-junit5 to your project. It provides those parts that make JUsecase truely awesome for TDD.

Easy to test

Imagine a very common usecase: A user logs in at your system.

public class Login implements Usecase<Login.Request, Login.Response> {
    public static class Request {
        public String name;
        public String password;

    public static class Response {
        public String welcomeMessage;

    public Response execute(Request request) {
        return null;

With JUsecase, writing the first failing test is very straight-forward.

public class LoginTest extends UsecaseTest<Request, Response> {
    public void setUp() {
        usecase = new Login(); = "John";
        request.password = "password";
    public void nullName() { = null;
        thenErrorMessageIs("Name must not be empty");

Notice that no boilerplate code needs to be written. The UsecaseTest class provides you with methods to execute the request as well as some assertions. It also automatically creates the request object for you. The only thing you need to do in the setup is to create the usecase and set some values for your "golden" request.

To make the test in the example green, you would simply write:

public Response execute(Request request) {
    throw new RuntimeException("Name must not be empty");

In a real world usecase you would throw a custom exception that better suits the project you are actually working with. Error handling is completely up to you and your requirements.

Built in support for dependency injection

Dependency injection details are completely hidden from your application and managed at one central place.

For instance, you might want to have a central class that encapsulates your business logic. In terms of JUsecase, this would be a UsecaseExecutor:

public interface UsecaseExecutor {
    <Request, Response> Response execute(Request request);

This means the main module (UI, REST, TimerBean, ...) fires a request at the UsecaseExecutor. Which class is exactly executing the request doesn't concern the main module. For example a REST call could come down to this:

public Login.Response login(Login.Request request) {
    return businessLogic.execute(request);

And to be sure that the request can actually be executed by the business logic, we add this unit test:

public class BusinessLogicTest extends UsecaseExecutorTest {
    public void requestsCanBeExecuted() {
        givenExecutor(new BusinessLogic());
        // ... in a real world application more usecases will follow here.

This test will become green by implementing a simple Logic class.

public class BusinessLogic extends ManualUsecaseExecutor {
    public BusinessLogic() {
        addUsecase(new Login());

Manual dependency injection

Note that the above class derives from ManualUsecaseExecutor. This means all dependency injection is done by hand.

Imagine an application with a lot of usecases. Yet easy to implement, a problem of the above solution is that all usecases are generated instantly. In a real world application every usecase would require lower level dependencies like entity gateways or external plugins. In case you want to achieve that a concrete usecase is only created when it is actually used, you can provide a factory instead:

public class BusinessLogic extends ManualUsecaseExecutor {
    public BusinessLogic() {
        addUsecase(new Factory<Login>() {
            public Login create() {
                return new Login();

Or in Java 8 lambda notation:

public class BusinessLogic extends ManualUsecaseExecutor {
    public BusinessLogic() {
        addUsecase(() -> new Login());

Notice that absolutely nothing needs to be changed in your unit test when switching to usecase factories.

Automatic dependency injection

It is also possible to use a dependency injection framework with JUsecase. Have a look at the BusinessLogic class derived from GuiceUsecaseExecutor.

public class BusinessLogic extends GuiceUsecaseExecutor {
    public BusinessLogic() {

Note: JUsecase does not come with Guice support by default. To add guice support, follow the instructions on jusecase-guice. This should also be a good starting point if you would like to add support for other dependency injection frameworks.

Note: There currently is no golden way to use dependency injection. Both manual and automatic approaches have benefits and drawbacks. This is why JUsecase doesn't require you to make that decision at the beginning of your project.

If you are unsure what dependency injection method to use, start with manual injection. The good news is: Switching to a dependency injection framework will still be very straight forward. This is because all dependencies are managed at one central place. And remember, no tests need to be changed when you change the dependency injection method. Your unit tests will check that your usecases can be generated, no matter what.


Java framework for a usecase centric architecture as proposed by Robert C. Martin and others.







No packages published