Initializing Apache Beam Test Pipeline in Scala fails - java

When I try to run a test pipeline it raise an error
here is the source code to create the test pipeline:
val p: TestPipeline = TestPipeline.create()
and here is the error :
java.lang.IllegalStateException: Is your TestPipeline declaration missing a #Rule annotation? Usage: #Rule public final transient TestPipeline pipeline = TestPipeline.create();

I have solve the problem by following code :
val p: TestPipeline = TestPipeline.create().enableAbandonedNodeEnforcement(false)

Related

NoClassDefFoundError thrown during runtime on Kotest Unit Test

I'm getting this NoClassDefFoundError during run time (Run test via Intellij or via gradlew test)
Here is my simple test (new to Kotlin and Kotest)
class AuthenticationValidatorServiceTest : StringSpec({
val userRepository = mockk<UserRepository>()
val userTypeRepository = mockk<UserTypeRepository>()
val accessTokenRepository = mockk<AccessTokenRepository>()
lateinit var service: AuthenticationValidatorService
beforeTest {
service = AuthenticationValidatorService(userRepository, userTypeRepository, accessTokenRepository)
}
"Should throw ResourceNotFoundException" {
shouldThrow<ResourceNotFoundException> {
val userId = UUID.randomUUID()
every { userRepository.findById(userId) } returns Optional.empty()
service.validateUser(userId)
}
}
})
Is there something that I miss setting up on my gradle file?
I just added the dependencies that this
tasks.withType(Test) {
useJUnitPlatform()
}
Getting these errors
> Task :test
WARNING: ExecutionInvokerPatcher failed: java.lang.ClassNotFoundException: org.junit.jupiter.engine.descriptor.ClassTestDescriptor
Expected exception lock.http.exception.ResourceNotFoundException but a NoClassDefFoundError was thrown instead.
java.lang.AssertionError: Expected exception lock.http.exception.ResourceNotFoundException but a NoClassDefFoundError was thrown instead.
at lock.http.service.AuthenticationValidatorServiceTest$1$2.invokeSuspend(AuthenticationValidatorServiceTest.kt:54)
at lock.http.service.AuthenticationValidatorServiceTest$1$2.invoke(AuthenticationValidatorServiceTest.kt)
at lock.http.service.AuthenticationValidatorServiceTest$1$2.invoke(AuthenticationValidatorServiceTest.kt)
at io.kotest.core.spec.style.scopes.StringSpecRootContext$invoke$1.invokeSuspend(StringSpecRootContext.kt:66)
at io.kotest.core.spec.style.scopes.StringSpecRootContext$invoke$1.invoke(StringSpecRootContext.kt)
at io.kotest.core.spec.style.scopes.StringSpecRootContext$invoke$1.invoke(StringSpecRootContext.kt)
at io.kotest.core.internal.ExecutionsKt$executeWithBehaviours$2$1.invokeSuspend(executions.kt:13)
at io.kotest.core.internal.ExecutionsKt$executeWithBehaviours$2$1.invoke(executions.kt)
at io.kotest.core.internal.ExecutionsKt$executeWithBehaviours$2$1.invoke(executions.kt)
at io.kotest.core.internal.ExecutionsKt.wrapTestWithGlobalAssert(executions.kt:39)
at io.kotest.core.internal.ExecutionsKt.access$wrapTestWithGlobalAssert(executions.kt:1)
at io.kotest.core.internal.ExecutionsKt$executeWithBehaviours$2.invokeSuspend(executions.kt:12)
at io.kotest.core.internal.ExecutionsKt$executeWithBehaviours$2.invoke(executions.kt)
at io.kotest.core.internal.ExecutionsKt$executeWithBehaviours$2.invoke(executions.kt)
at io.kotest.core.internal.ExecutionsKt$wrapTestWithAssertionModeCheck$2.invokeSuspend(executions.kt:25)
at io.kotest.core.internal.ExecutionsKt$wrapTestWithAssertionModeCheck$2.invoke(executions.kt)
at io.kotest.core.internal.ExecutionsKt$wrapTestWithAssertionModeCheck$2.invoke(executions.kt)
at io.kotest.core.internal.AssertionsCheckKt.executeWithAssertionsCheck(assertionsCheck.kt:25)
at io.kotest.core.internal.ExecutionsKt.wrapTestWithAssertionModeCheck(executions.kt:24)
at io.kotest.core.internal.ExecutionsKt.executeWithBehaviours(executions.kt:11)
at io.kotest.core.internal.TestCaseExecutor$executeInScope$2.invokeSuspend(TestCaseExecutor.kt:268)
at io.kotest.core.internal.TestCaseExecutor$executeInScope$2.invoke(TestCaseExecutor.kt)
at io.kotest.core.internal.TestCaseExecutor$executeInScope$2.invoke(TestCaseExecutor.kt)

Java 11 junit jupiter assertThrows

I try to migrate from Java 8 to 11 and get an error in my test class that I don't understand.
My failing (groovy) test is:
#SpringJUnitConfig
class TestSpringBeanScopeChecker {
#Autowired
ApplicationContext ctx
#Test
void testSingletonFail() {
Assertions.assertThrows(IllegalStateException.class) {
SpringBeanScopeChecker.check(ctx, DummyPrototype.class, BeanDefinition.SCOPE_SINGLETON)
}
}
}
The SpringBeanScopeChecker:
public class SpringBeanScopeChecker {
private SpringBeanScopeChecker() {}
public static void check(ApplicationContext ctx, Class<?> type, String scope)
throws IllegalStateException {
AbstractApplicationContext actx = (ctx instanceof AbstractApplicationContext) ?
((AbstractApplicationContext) ctx) :
new StaticApplicationContext(ctx);
ConfigurableListableBeanFactory factory = actx.getBeanFactory();
for (String key : ctx.getBeanNamesForType(type)) {
BeanDefinition definition = factory.getMergedBeanDefinition(key);
if (!scope.equals(definition.getScope())) {
throw new IllegalStateException(
"Every spring bean "
+ "must be request scoped in the bean configuration. The current scope is: "
+ definition.getScope());
}
}
}
}
So for the test I'm expecting a IllegalArgumentException. And this is working fine with Java8.
When I switch to Java11 and execute the test I get this error:
[ERROR] testSingletonFail Time elapsed: 0.009 s <<< FAILURE!
org.opentest4j.AssertionFailedError: Unexpected exception type thrown
==> expected: <java.lang.IllegalStateException> but was: <java.lang.AbstractMethodError>
at TestSpringBeanScopeChecker.testSingletonFail(TestSpringBeanScopeChecker.groovy:22)
Caused by: java.lang.AbstractMethodError: Receiver class
TestSpringBeanScopeChecker does not define or inherit an
implementation of the resolved method 'abstract java.lang.Object
getProperty(java.lang.String)' of interface groovy.lang.GroovyObject.
at TestSpringBeanScopeChecker.testSingletonFail(TestSpringBeanScopeChecker.groovy:22)
In case someone else has the same problem I write down the solution for this.
The problem was misconfiguration of the groovy-eclipse-compiler and groovy-eclipse-batch.
My groovy version is managed by spring-boot and I didn't update the groovy-eclipse-batch according to the groovy.version from the spring-boot pom.
According to this issue on github:
You have to compile with groovy-eclipse-batch and groovy runtime in the same version. groovy-eclipse-batch and groovy runtime should be matched up. E.g. batch 2.5.10-0x and runtime 2.5.10 or batch 3.0.1-0x and runtime 3.0.1.

Camel : ClassCastException: DefaultMessage cannot be cast to class SnmpMessage

I'm getting the following ClassCastException from a unit test when I try to call testTemplate.sendBody(String, Object) below:
SnmpRoute.kt
.process { exchange ->
val message = exchange.getIn() as SnmpMessage
SnmpRouteTest.kt
#RunWith(CamelSpringBootRunner::class)
#CamelSpringBootTest
#SpringBootTest
#DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
#DisableJmx(false)
#MockEndpoints("log:*")
class SnmpRouteTest {
object SnmpConstants {
const val SNMP_TRAP = "<snmp><entry><oid>1.3.6.1.2.1.1.3.0</oid><value>6 days, 3:44:57.82</value></entry><entry><oid>1.3.6.1.6.3.1.1.4.1.0</oid><value>1.3.6.1.4.1.8072.2.3.0.1</value></entry><entry><oid>1.3.6.1.4.1.8072.2.3.2.1</oid><value>123456</value></entry></snmp>"
}
#Autowired
lateinit var camelContext: CamelContext
#Produce
lateinit var testTemplate: ProducerTemplate
...
...
#Test
#Throws(Exception::class)
fun testSnmpRoute() {
AdviceWithRouteBuilder.adviceWith(camelContext, "CamelSnmpTrapRoute") { routeBuilder -> routeBuilder.replaceFromWith(SnmpConstants.DIRECT_SNMP_ENDPOINT) }
testTemplate.sendBody(SnmpConstants.DIRECT_SNMP_ENDPOINT, SnmpConstants.SNMP_TRAP)
...
}
}
Exception
java.lang.ClassCastException: class org.apache.camel.support.DefaultMessage cannot be cast to class
org.apache.camel.component.snmp.SnmpMessage (org.apache.camel.support.DefaultMessage and org.apache.
camel.component.snmp.SnmpMessage are in unnamed module of loader 'app')
I have tried to construct an SnmpMessage object and use that in the sendBody() call, as when I test this route manually with the snmptrap utility, I see the following in the logs:
Get In[SnmpMessage: <snmp><entry><oid>1.3.6.1.2.1.1.3.0</oid><value>12 days, 8:40:47.70</value></entry><entry><oid>1.3.6.1.6.3.1.1.4.1.0</oid><value>1.3.6.1.4.1.8072.2.3.0.1</value></entry><entry><oid>1.3.6.1.4.1.8072.2.3.2.1</oid><value>123456</value></entry></snmp>]
However I'm getting the same issue with this approach.
I am using Apache Camel v3.0.0-RC3
Thanks to #ShellDragon for help so far with this.
Your processor is casting to a SmppMessage, but your unit test, replaces the consumer (from endpoint) from a smpp to direct component and therefore the message implementation is DefaultMessage.
This is what worked for me. I needed to overwrite the exchange.getIn() message with an SnmpMessage, and add a PDU object, rather than the XML String block.
#CamelSpringBootTest
#SpringBootTest(classes = [SnmpTrapReceiverApplication::class])
#DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
#DisableJmx(false)
#ExtendWith(MockitoExtension::class)
#EnableAutoConfiguration
class SnmpTrapRouteTest {
#MockBean
lateinit var repo: PojoRepo
#Produce
lateinit var producerTemplate: ProducerTemplate
#Autowired
lateinit var camelContext: CamelContext
#Test
#Throws(Exception::class)
fun testSnmpRoute() {
AdviceWithRouteBuilder.adviceWith(camelContext, "snmp-trap-route") { routeBuilder ->
routeBuilder.replaceFromWith("direct:snmp-from")
}
// Create a PDU object to send to the SNMP endpoint, rather than SNMP XML
val trap = PDU()
trap.type = PDU.TRAP
trap.requestID = Integer32(123456789)
trap.add(VariableBinding(OID("1.2.3.4.5"), OctetString("snmp-trap-payload")))
// Create a new DefaultExchange and add an SnmpMessage object as the in-message,
// constructed with the camelContext and the PDU object
val exchange = DefaultExchange(camelContext)
exchange.setIn(SnmpMessage(camelContext, trap))
producerTemplate.setDefaultEndpointUri("direct:snmp-from")
producerTemplate.send(exchange)
verify(repo, atLeast(1)).save(any())
}
}
Since I am using JUnit 5, I changed #RunWith(CamelSpringBootRunner::class) to #CamelSpringBootTest, as after downloading the 3.0.0-RC3 release source, I found a comment saying to do so.

How to remove custome properties from SOAPUI test case using java?

I have some custom properties for all the test cases in SoapUI.
I am able to delete using Groovy script step as described in below question:
How to remove Custom Properties from a SoapUI TestCase using Groovy?
testRunner.testCase.removeProperty( "Testcase_Property" );
But I wanted to delete these properties from JAVA. Below is the code I wrote:
String soapuiProjectPath = "ProjectLocation";
WsdlProject project = new WsdlProject(soapuiProjectPath);
StringToObjectMap context = new StringToObjectMap();
TestSuite testSuite = project.getTestSuiteByName("TestSuiteName");
WsdlTestSuite wsdlSuite = (WsdlTestSuite) testSuite;
List<TestCase> allTestCaseList = wsdlSuite.getTestCaseList();
for (TestCase testCase : allTestCaseList) {
WsdlTestCaseRunner testCaseRunner = new WsdlTestCaseRunner((WsdlTestCase) testCase, context);
List<TestProperty> testCasePropertyList = testCase.getPropertyList();
for (TestProperty testProperty : testCasePropertyList) {
WsdlTestRunContext runContext = testCaseRunner.getRunContext();
runContext.removeProperty(testProperty.getName());
}
}
System.out.println("Completed execution.");
project.save();
It is not throwing any exception. But not actually removing the custom properties as well.
Because you've to apply the removeProperty in WsdlTestCase not in WsdlTestRunContext. You can change your testCase loop code for something like:
for(TestCase testCase : allTestCaseList) {
List<TestProperty> testCasePropertyList = testCase.getPropertyList();
for (TestProperty testProperty : testCasePropertyList) {
((WsdlTestCase) testCase).removeProperty(testProperty.getName());
}
}
Hope it helps,

Spring Boot can't run single test in IntelliJ

This started happening recently, but I'm not sure what changed to cause it.
When I run all tests from IntelliJ, all is well. Also the gradle build is fine.
When I run a single unit test, all is well.
When I run a single web integration test, it fails because a config class has all null properties.
The config class looks like (Kotlin):
#Component
#ConfigurationProperties(prefix = "api")
public open class ApiConfigImpl : ApiConfig
{
A test looks like:
#RunWith(SpringJUnit4ClassRunner::class)
#ContextConfiguration(classes = arrayOf(ApplicationAssembly::class), loader = SpringApplicationContextLoader::class)
#WebIntegrationTest
open class CandidateProfileControllerTest
{
#Inject lateinit var profileRepo: CandidateProfileRepository
//etc a few more deps used to setup test data
#Test
open fun getById()
{
val greg = CandidateProfile("123", "12312", "Greg", "Jones", dateOfBirth = Date(), gender = Gender.MALE,
biography = "ABC", maxMatchableAge = null, maxMatchableDistance = null)
profileRepo.save(greg)
val auth = given().header("content-type", "application/json")
.body(testCredentials)
.post("/authorization/social").peek().asString()
val accessToken: String = from(auth).get("accessToken")
given().header("Access-Token", accessToken).
header("API-Key", testAPIKey()).
get("/profile/${greg.id}").
peek().then().
body("stageName", notNullValue())
}
I'm not sure what information I can add. Based on the limited information provided:
Is this a known problem with a known solution?
This is a bug, logged in the IntelliJ/Kotlin tracker, with a pending fix.

Categories

Resources