Sandbox

Add this URL to your Eclipse Installation to reach this solution's update site.

2025-03 (4.35), 2024-12 (4.34), 2025-06 (4.36)

https://github.com/carstenartur/sandbox/raw/main/sandbox1.2.1

Learn more...
Solution Description

Update java test code to Junit5 

package test;

import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
        MyTest.class
})
public class MyTest {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        // Setup vor allen Tests
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        // Aufräumen nach allen Tests
    }

    @Before
    public void setUp() throws Exception {
        // Setup vor jedem Test
    }

    @After
    public void tearDown() throws Exception {
        // Aufräumen nach jedem Test
    }

    @Ignore("Ignored with message")
    @Test
    public void ignoredTestWithMessage() {
        Assert.fail("This test is ignored with a message.");
    }

    @Ignore
    @Test
    public void ignoredTestWithoutMessage() {
        Assert.fail("This test is ignored without a message.");
    }

    @Test
    public void testBasicAssertions() {
        Assert.assertEquals("Values should match", 42, 42);
        Assert.assertEquals(42, 42);

        Assert.assertNotEquals("Values should not match", 42, 43);
        Assert.assertNotEquals(42, 43);

        Object o = "test";
        Assert.assertSame("Objects should be same", o, o);
        Assert.assertSame(o, o);

        Assert.assertNotSame("Objects should not be same", "test1", "test2");
        Assert.assertNotSame("test1", "test2");

        Assert.assertNull("Should be null", null);
        Assert.assertNull(null);

        Assert.assertNotNull("Should not be null", new Object());
        Assert.assertNotNull(new Object());

        Assert.assertTrue("Condition should be true", true);
        Assert.assertTrue(true);

        Assert.assertFalse("Condition should be false", false);
        Assert.assertFalse(false);
    }

    @Test
    public void testArrayAssertions() {
        int[] expected = {1, 2, 3};
        int[] actual = {1, 2, 3};

        Assert.assertArrayEquals("Arrays should match", expected, actual);
        Assert.assertArrayEquals(expected, actual);
    }

    @Test
    public void testWithAssume() {
        Assume.assumeTrue("Precondition failed", true);
        Assume.assumeTrue(true);

        Assume.assumeFalse("Precondition not met", false);
        Assume.assumeFalse(false);

        Assume.assumeNotNull("Value should not be null", new Object());
        Assume.assumeNotNull(new Object());

        Assume.assumeThat("Value should match condition", 42, CoreMatchers.is(42));
        Assume.assumeThat(42, CoreMatchers.is(42));
    }

    @Test
    public void testAssertThat() {
        Assert.assertThat("Value should match", 42, CoreMatchers.is(42));
        Assert.assertThat(42, CoreMatchers.is(42));

        Assert.assertThat("String should contain value", "Hello, world!", CoreMatchers.containsString("world"));
        Assert.assertThat("Hello, world!", CoreMatchers.containsString("world"));
    }

    @Test
    public void testEqualityWithDelta() {
        Assert.assertEquals("Floating point equality with delta", 0.1 + 0.2, 0.3, 0.0001);
        Assert.assertEquals(0.1 + 0.2, 0.3, 0.0001);

        Assert.assertNotEquals("Floating point inequality", 0.1 + 0.2, 0.4, 0.0001);
        Assert.assertNotEquals(0.1 + 0.2, 0.4, 0.0001);
    }

    @Test
    public void testFail() {
        // Testfälle für Assert.fail
        Assert.fail("This test should fail with a message.");
        Assert.fail();
    }
}

Refactor it to

package test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.junit.MatcherAssume.assumeThat;

import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses({
        MyTest.class
})
public class MyTest {

    @BeforeAll
    public static void setUpBeforeClass() throws Exception {
        // Setup vor allen Tests
    }

    @AfterAll
    public static void tearDownAfterClass() throws Exception {
        // Aufräumen nach allen Tests
    }

    @BeforeEach
    public void setUp() throws Exception {
        // Setup vor jedem Test
    }

    @AfterEach
    public void tearDown() throws Exception {
        // Aufräumen nach jedem Test
    }

    @Disabled("Ignored with message")
    @Test
    public void ignoredTestWithMessage() {
        Assertions.fail("This test is ignored with a message.");
    }

    @Disabled
    @Test
    public void ignoredTestWithoutMessage() {
        Assertions.fail("This test is ignored without a message.");
    }

    @Test
    public void testBasicAssertions() {
        Assertions.assertEquals(42, 42, "Values should match");
        Assertions.assertEquals(42, 42);

        Assertions.assertNotEquals(42, 43, "Values should not match");
        Assertions.assertNotEquals(42, 43);

        Object o = "test";
        Assertions.assertSame(o, o, "Objects should be same");
        Assertions.assertSame(o, o);

        Assertions.assertNotSame("test1", "test2", "Objects should not be same");
        Assertions.assertNotSame("test1", "test2");

        Assertions.assertNull(null, "Should be null");
        Assertions.assertNull(null);

        Assertions.assertNotNull(new Object(), "Should not be null");
        Assertions.assertNotNull(new Object());

        Assertions.assertTrue(true, "Condition should be true");
        Assertions.assertTrue(true);

        Assertions.assertFalse(false, "Condition should be false");
        Assertions.assertFalse(false);
    }

    @Test
    public void testArrayAssertions() {
        int[] expected = {1, 2, 3};
        int[] actual = {1, 2, 3};

        Assertions.assertArrayEquals(expected, actual, "Arrays should match");
        Assertions.assertArrayEquals(expected, actual);
    }

    @Test
    public void testWithAssume() {
        Assumptions.assumeTrue(true, "Precondition failed");
        Assumptions.assumeTrue(true);

        Assumptions.assumeFalse(false, "Precondition not met");
        Assumptions.assumeFalse(false);

        Assumptions.assumeNotNull(new Object(), "Value should not be null");
        Assumptions.assumeNotNull(new Object());

        assumeThat("Value should match condition", 42, CoreMatchers.is(42));
        assumeThat(42, CoreMatchers.is(42));
    }

    @Test
    public void testAssertThat() {
        assertThat("Value should match", 42, CoreMatchers.is(42));
        assertThat(42, CoreMatchers.is(42));

        assertThat("String should contain value", "Hello, world!", CoreMatchers.containsString("world"));
        assertThat("Hello, world!", CoreMatchers.containsString("world"));
    }

    @Test
    public void testEqualityWithDelta() {
        Assertions.assertEquals(0.1 + 0.2, 0.3, 0.0001, "Floating point equality with delta");
        Assertions.assertEquals(0.1 + 0.2, 0.3, 0.0001);

        Assertions.assertNotEquals(0.1 + 0.2, 0.4, 0.0001, "Floating point inequality");
        Assertions.assertNotEquals(0.1 + 0.2, 0.4, 0.0001);
    }

    @Test
    public void testFail() {
        // Testfälle für Assert.fail
        Assertions.fail("This test should fail with a message.");
        Assertions.fail();
    }
}

 

Set explicit encoding

 

Some java api has been designed originally without a strong focus on safety on usage of different charsets. Because of that apis have simple string parameters to determine the encoding to use or even a default encoding that is based on the environment settings. The situation has improved a lot and you should explicitly set the charset. In doing so for type safety better switch to Charset class.

For an example what the result looks similar to look at
58ae341

That is what this cleanup is trying to support.

Starting with Java 10

1. 

ByteArrayOutputStream ba=new ByteArrayOutputStream();
String result=ba.toString();

Case 1: Option "keep_behavior"

ByteArrayOutputStream ba=new ByteArrayOutputStream();
String result=ba.toString(Charset.defaultCharset());

Case 2: Option "enforce_utf8"

ByteArrayOutputStream ba=new ByteArrayOutputStream();
String result=ba.toString(StandardCharsets.UTF_8);

2.

ByteArrayOutputStream ba=new ByteArrayOutputStream();
String result=ba.toString("UTF-8");

ByteArrayOutputStream ba=new ByteArrayOutputStream();
String result=ba.toString(StandardCharsets.UTF_8);

 

Starting with Java 10

Reader r=Channels.newReader(ch,"UTF-8");
Reader r=Channels.newReader(ch,StandardCharsets.UTF_8);

 

Starting with Java 10

Channels.newWriter(ch,"UTF-8");
Channels.newWriter(ch,StandardCharsets.UTF_8);

 

Starting with Java 18
Currently, jdt.ui supports test environments only up to Java 17, which means tests cannot be executed for this version.

1.

Charset.forName("UTF-8");
StandardCharsets.UTF_8;

2.

Charset.forName("UTF-16");
StandardCharsets.UTF_16;

 

Starting with Java 5

1.

new java.util.Formatter(new File(), "UTF-8");
new java.util.Formatter(new File(), StandardCharsets.UTF_8);

2.

new java.util.Formatter(new File(), "UTF-8",new java.util.Locale());
new java.util.Formatter(new File(), StandardCharsets.UTF_8,new java.util.Locale());

...

 

Starting with Java 5

new InputStreamReader(InputStream in, "UTF-8");
new InputStreamReader(InputStream in, StandardCharsets.UTF_8);

 

Starting with Java 5

new OutputStreamWriter os=new OutputStreamWriter(InputStream in,"UTF-8");
new OutputStreamWriter os=new OutputStreamWriter(InputStream in, StandardCharsets.UTF_8);

 

Starting with Java 5

Stream fw=new PrintStream("file.txt", "UTF-8");
Stream fw=new PrintStream("file.txt", StandardCharsets.UTF_8);

 

Starting with Java 10

Properties.storeToXML(java.io.OutputStream,"UTF-8");
Properties.storeToXML(java.io.OutputStream,StandardCharsets.UTF_8);

 

Starting with Java 10

new java.util.Scanner(new File("asdf"),"UTF-8");
new java.util.Scanner(new File("asdf"),StandardCharsets.UTF_8);

 

Starting with Java 10

String s=new String(byte[],"UTF-8");
String s=new String(byte[],StandardCharsets.UTF_8);

 

Starting with Java 5

String.getBytes("Utf-8")
String.getBytes(StandardCharsets.UTF_8);

 

Starting with Java 10

java.net.URLDecoder.decode("asdf","UTF-8");
java.net.URLDecoder.decode("asdf",StandardCharsets.UTF_8);

Tags: junit, cleanup

Additional Details

Eclipse Versions: 2025-03 (4.35), 2024-12 (4.34), 2025-06 (4.36)

Platform Support: Windows, Mac, Linux/GTK

Development Status: Alpha

Date Created: Sunday, December 15, 2024 - 16:49

License: EPL 2.0

Date Updated: Tuesday, January 28, 2025 - 14:10

Submitted by: Carsten Hammer

Screenshot
Date Ranking Installs Clickthroughs
April 2025 507/619 3 9
March 2025 532/661 4 7
February 2025 306/645 34 18
January 2025 0/0 0 21
December 2024 567/654 2 24
November 2024 0/0 0 0
October 2024 0/0 0 0
September 2024 0/0 0 0
August 2024 0/0 0 0
July 2024 0/0 0 0
June 2024 0/0 0 0
May 2024 0/0 0 0
View Data for all Listings

Unsuccessful Installs

Unsuccessful Installs in the last 7 Days: 0

Download last 500 errors (CSV)

Marketplace Drag to Install Button

By adding the following code below to your website you will be able to add an install button for Sandbox.

HTML Code:

Markdown Syntax:

Output:

Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client

Reviews Add new review