Groovy "Contains" function deprecated


I recently got a notification that the “contains” function in groovy was deprecated. Does anyone know of a replacement for it?

I made a test case in Groovy lang:

import java.lang.CharSequence
String text = "Hello, World!"
println "text contains it: " + text.contains("World!")
if (text instanceof CharSequence) {
    println "text of type java.lang.String is an instance of java.lang.CharSequece"
    CharSequence charseq = (CharSequence)text
    println "charseq contains it: " + charseq.contains("World!")

When I execute it, I got the following output in the log:

text contains it: true
text of type java.lang.String is an instance of java.lang.CharSequece
charseq contains it: true

This experiment proves that the following code is a valid Groovy statement:


And I believe it will stay valid in future. Yo do not have to worry about the notification you got too much.

I will tell you the reason why in the following comments.


Look at the following screenshot. Please note that the method name “contains” is stroke out by IDE. I suppose that the notification you got is talking about this IDE’s behavior.

You do not have to worry about it that contains is struck out. I will tell you why next

Please find the small window is saying that by calling the contains method of the text variable you effectively call the contains method of the org.codehause.groovy.runtime.StringGroovyMethods class. This is a trick done by Groovy language.


1 Like

Have a look at the Groovy lang API document:

The contains(java.lang.String self, java.lang.String text) method is , Yes, Deprecated. In this sense, IDE is working right. Striking out contains is grammatically correct. Though I find it unnecessary and a bit annoying.

The document says “Use the CharSequence version”. The contains(java.lang.CharSequence self, java.lang.CharSequence text) is not deprecated, will be avaialbe for long term use.

The java.lang.String class implements the java.lang.CharSequence interface. Therefore a Groovy statement:


will stay valid in future.

You can safely ignore it if contains is struck out by IDE.



Thank you it worked. :slight_smile:

You can rewrite this:

String text = "Hello, World"
println "text contains it: " + text.contains("World")

to the following:

CharSequence text2 = "Hello, World"
println "text2 contains it: " + text2.contains("World")

or to this:

String text2 = "Hello, World"
println "text2 contains it: " + ((CharSequence)text2).contains("World")

then you would be able to avoid contains being struck-out.

It’s depreciated but it’s a bug of eclipse. So don’t worry.
You can refer this.


When I use the “contains” function, it returns error below:

import java.lang.CharSequence as CharSequence
CharSequence CurrentDay = Mobile.getAttribute(findTestObject(‘CurrentDate’), ‘label’, 1)
Mobile.verifyElementText(findTestObject(‘CurrentDate’), CurrentDay.contains(“Wednesday”))

groovy.lang.MissingMethodException: No signature of method: static is applicable for argument types: (com.kms.katalon.core.testobject.TestObject, java.lang.Boolean) values: [TestObject - ‘Object Repository/CurrentDay_Monday’, true]
Possible solutions: verifyElementText(com.kms.katalon.core.testobject.TestObject, java.lang.String), verifyElementText(com.kms.katalon.core.testobject.TestObject, java.lang.String, com.kms.katalon.core.model.FailureHandling), verifyElementExist(com.kms.katalon.core.testobject.TestObject, int)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(
at com.kms.katalon.core.main.TestCaseExecutor.runScript(
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(
at com.kms.katalon.core.main.TestCaseExecutor.execute(
at com.kms.katalon.core.main.TestCaseMain.runTestCase(
at com.kms.katalon.core.main.TestCaseMain.runTestCase(
at com.kms.katalon.core.main.TestCaseMain$runTestCase$ Source)