1

I'm trying to migration from JUnit4 to JUnit5 and I have a @Rule annotation and I don't exactly know how I can replace this. I tried @ExtendWith but not working for me.

My code with JUnit 4 annotations:

 @Rule public TextReport textReport = new TextReport(); @Rule public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer() .withCapabilities(new ChromeOptions()); 

Edit: The part of my current code is:

@Testcontainers @ExtendWith(TextReportExtension.class) public class TestBase { @Container public static BrowserWebDriverContainer chrome = new BrowserWebDriverContainer() .withCapabilities(new ChromeOptions()); @Before public void setup() { RemoteWebDriver driver = chrome.getWebDriver(); System.out.println("VNC Address: " + chrome.getVncAddress()); 

All test classes extends TestBase. And I get an error:

java.lang.IllegalStateException: Mapped port can only be obtained after the container is started 
2
  • I've updated my answer and I guess the error comes from mixing JUnit 4 & 5 in the same test. Commented Jul 10, 2020 at 7:01
  • can you post TextReportExtension.class Commented Mar 24, 2021 at 17:39

1 Answer 1

1

Testcontainers offers JUnit 5 support by adding the following dependency to your project:

<dependency> <groupId>org.testcontainers</groupId> <artifactId>junit-jupiter</artifactId> <version>1.14.3</version> <scope>test</scope> </dependency> 

This allows you to refactor BrowserWebDriverContainer to the following:

@Testcontainers public class YourTest { @Container public static BrowserWebDriverContainer chrome = new BrowserWebDriverContainer() .withCapabilities(new ChromeOptions()); } 

For the TextReport rule, simply add the following extension from selenide to your class and it will capture the console for you:

@Testcontainers @ExtendWith(TextReportExtension.class) public class YourTest { } 

UPDATE: Make sure to always either use JUnit 4 or JUnit 5 in your test. @Before is from JUnit 4, try to replace it with @BeforeEach and ensure your @Test is coming from import org.junit.jupiter.api.Test;.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.