Home JUnit 5 Extension
Post
Cancel

JUnit 5 Extension

JUnit 5 Extension

JUnit 5 Extensions are a powerful feature that allows you to extend the behavior of JUnit 5 tests. Extensions can be used to add new functionality to your tests, such as custom reporting, data injection, or mocking.

In this blog post, we will explore how to use JUnit 5 Extensions and provide some examples of how they can be used.

Creating a JUnit 5 Extension

Creating a JUnit 5 Extension is very simple. You simply need to create a class that implements the Extension interface. The Extension interface has no methods, so you do not need to implement any methods.

1
2
3
public class MyExtension implements Extension {
// Extension code goes here
}

Using a JUnit 5 Extension

Once you have created your JUnit 5 Extension, you can use it in your test class by annotating your test class or test method with the @ExtendWith annotation.

1
2
3
4
@ExtendWith(MyExtension.class)
public class MyTestClass {
// Test methods go here
}

If you want to apply an extension to a specific test method, you can annotate the method with the @ExtendWith annotation.

1
2
3
4
5
6
7
public class MyTestClass {
@Test
@ExtendWith(MyExtension.class)
public void myTest() {
// Test code goes here
}
}

JUnit 5 provides several built-in extensions that can be used out of the box. Here are a few examples:

ParameterResolver

The ParameterResolver extension allows you to inject parameters into your test methods. This is useful when you want to use the same object in multiple tests.

1
2
3
4
5
6
7
public class MyTestClass {
@Test
void myTest(@MyParameter MyObject myObject) {
// Test code goes here
}
}
1
2
3
4
5
6
7
8
9
10
11
12
public class MyParameterResolver implements ParameterResolver {
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
return parameterContext.getParameter().getType() == MyObject.class;
}

    @Override
    public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
        return new MyObject();
    }
}

TestWatcher

The TestWatcher extension allows you to add custom behavior before or after each test method. This is useful when you want to add custom reporting or logging.

1
2
3
4
5
6
7
8
9
10
11
public class MyTestWatcher extends TestWatcher {
@Override
protected void succeeded(ExtensionContext context) {
System.out.println("Test succeeded: " + context.getDisplayName());
}

    @Override
    protected void failed(Throwable e, ExtensionContext context) {
        System.out.println("Test failed: " + context.getDisplayName());
    }
}
1
2
3
4
5
6
7
8
@ExtendWith(MyTestWatcher.class)
public class MyTestClass {
@Test
void myTest() {
// Test code goes here
}
}

MockMvcExtension

The MockMvcExtension extension allows you to use the Spring MVC Test framework with JUnit 5 tests. This is useful when you want to test your Spring MVC controllers.

1
2
3
4
5
6
7
8
9
10
11
12
@ExtendWith(MockMvcExtension.class)
@WebMvcTest(MyController.class)
public class MyControllerTest {
@Autowired
private MockMvc mockMvc;

    @Test
    void myTest() throws Exception {
        mockMvc.perform(get("/myendpoint"))
               .andExpect(status().isOk());
    }
}

Conclusion

JUnit 5 Extensions are a powerful feature that allows you to extend the behavior of JUnit 5 tests. By creating your own extensions or using the built-in extensions, you can add new functionality to your tests, such as custom reporting, data injection, or mocking. By using JUnit 5 Extensions, you can write more

This post is licensed under CC BY 4.0 by the author.