Alice Rossi
Alice Rossi

Reputation: 73

Convert an int to Hex and check the assertion. Java AT

I have a task to write an @Test annotated method which uses Integer toHexString and asserts that 11 becomes b

This is how I tried this getting illegal start of expression:

import org.junit.Test;
import org.junit.Assert;

public class HexTest {
    @Test
    public static void main (String[] args){
        static Integer.toHexString(11){
            Assert.assertEquals("int to Hex", 'b', 11);
        }
    }
}

I would be so grateful if someone could tell me what is wrong and how to fix it.

Upvotes: 2

Views: 466

Answers (2)

JustAnotherDeveloper
JustAnotherDeveloper

Reputation: 2256

The problem is that you don't need the static keyword to call a static method. What you should be doing is calling the method, storing the result in a variable and checking if that variable holds the value you expect:

import org.junit.Test;
import org.junit.Assert;

public class HexTest {
    @Test
    public void testToHexString(){
        String result = Integer.toHexString(11);
        Assert.assertEquals("int to Hex", "b", result);
    }
}

Upvotes: 0

Dmitry Rakovets
Dmitry Rakovets

Reputation: 589

Fixed:

import org.junit.Assert;
import org.junit.Test;

public class HexTest {
    @Test
    public void integerToHexStringTest() { // test method definition
        // GIVEN
        String expected = "b";
        
        // WHEN 
        String actual = Integer.toHexString(11); // call method `toHexString(11)`

        // THEN
        Assert.assertEquals("int to Hex", expected, actual);
    }
}

Upvotes: 1

Related Questions