Asked  4 Months ago    Answers:  5   Viewed   531 times

I have some TestNG code, where I am passing a Test annotation parameter called timeOut = TESTNG_TEST_TIMEOUT .

@Test(description = "Tests something.", groups = { "regression" }, 
   timeOut = TESTNG_TEST_TIMEOUT, enabled = true)

And in my TestBase class I have this member:

public final static long TESTNG_TEST_TIMEOUT = TimeUnit.MINUTES.toMillis(5);

When I use the above line of code, I get a 'attribute value must be constant' error in Eclipse.

But, if I simply define the member like so, it works:

public final static long TESTNG_TEST_TIMEOUT = 300000;

Is the use of TimeUnit not a constant?

 Answers

27

This

public final static long TESTNG_TEST_TIMEOUT = 300000;

is a constant variable, a type of constant expression.

This

public final static long TESTNG_TEST_TIMEOUT = TimeUnit.MINUTES.toMillis(5);

is not.

Annotation members expect constant expressions (and a few other things like enums and Class literals).

Friday, June 25, 2021
 
samayo
answered 4 Months ago
68

All your problems derive from this

byte[] encrypted = cipher.doFinal(toEncrypt.getBytes());
return encrypted;

Which are enclosed in a try, catch block, the problem is that in case the program found an exception you are not returning anything. Put it like this (modify it as your program logic stands):

public static byte[] encrypt(String toEncrypt) throws Exception{
    try{
        String plaintext = toEncrypt;
        String key = "01234567890abcde";
        String iv = "fedcba9876543210";

        SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE,keyspec,ivspec);
        byte[] encrypted = cipher.doFinal(toEncrypt.getBytes());

        return encrypted;
    } catch(Exception e){
        return null;            // Always must return something
    }
}

For the second one you must catch the Exception from the encrypt method call, like this (also modify it as your program logic stands):

public void actionPerformed(ActionEvent e)
  .
  .
  .
    try {
        byte[] encrypted = encrypt(concatURL);
        String encryptedString = bytesToHex(encrypted);
        content.removeAll();
        content.add(new JLabel("Concatenated User Input -->" + concatURL));

        content.add(encryptedTextField);
    setContentPane(content);
    } catch (Exception exc) {
        // TODO: handle exception
    }
}

The lessons you must learn from this:

  • A method with a return-type must always return an object of that type, I mean in all possible scenarios
  • All checked exceptions must always be handled
Tuesday, June 1, 2021
 
yosemite
answered 5 Months ago
28

You need to use dataproviderthreadcount. The threadpoolsize and invocationcount values are not required. See details here.

Saturday, July 3, 2021
 
jsuggs
answered 4 Months ago
74

The file needs to be called DNSLookUp.java and you need to put:

import java.net.InetAddress;
import java.net.UnknownHostException;    

At the top of the file

Tuesday, July 6, 2021
 
Kwadz
answered 4 Months ago
17

Your @BeforeMethod needs to be part of the group you are running.

You can also use @BeforeMethod(alwaysRun = true) if you don't want to hardcode the value of your group and if you think you will always want to run this method, regardless of the group you are currently running.

Saturday, September 18, 2021
 
noir
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :