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
Hubu999
  • 73
  • 2
  • 9

2 Answers2

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;.

rieckpil
  • 10,470
  • 3
  • 32
  • 56
0

@rieckpil thanks for answer! I did as you wrote it, but I have another problem now. I have a setup() class with the annotation @Before, setting the WebDriver and I get the following error:

java.lang.IllegalStateException: Mapped port can only be obtained after the container is started

My class:

    @Before
    public void setup() {
        RemoteWebDriver driver = chrome.getWebDriver();
        System.out.println("VNC Address: " + chrome.getVncAddress());
Hubu999
  • 73
  • 2
  • 9