Play Framework 2.2 - functional test fails with type mismatch - playframework-2.2

I've just upgraded to Play 2.2, and since the Helpers have changed, my test isn't compiling anymore.
import org.specs2.mutable.Specification
import play.api.test._
import play.api.test.Helpers._
import play.api.libs.ws._
import play.api.mvc.Results._
class ApplicationSpec extends Specification {
import controllers._
"Application" should {
"test WS logic" in new WithServer {
await(WS.url("http://localhost:3333").get()).status must equalTo(OK)
}
}
}
gives the following compile error
type mismatch;
[error] found : scala.concurrent.Future[play.api.libs.ws.Response]
[error] required: org.specs2.matcher.Matcher[?]

It's just a name clash between play.api.test.Helpers.await and org.specs2.matcher.FutureMatchers.await.
You could just refer to the play helper more explicitly (or rename your import):
Helpers.await(WS.url("http://localhost:3333").get()).status must equalTo(OK)
The following is probably better, however, which hasn't made it into the documentation yet:
https://github.com/playframework/playframework/blob/master/framework/src/play-test/src/main/scala/play/api/test/PlaySpecification.scala
So simply extend PlaySpecification instead of Specification in your test:
import org.specs2.mutable.Specification
import play.api.test._
import play.api.test.Helpers._
import play.api.libs.ws._
import play.api.mvc.Results._
class ApplicationSpec extends PlaySpecification {
import controllers._
"Application" should {
"test WS logic" in new WithServer {
await(WS.url("http://localhost:3333").get()).status must equalTo(OK)
}
}
}

Related

Ktor testing Fail to serialize body. Content has type: class ... but OutgoingContent expected

so trying to understand how I can make testing ktor app with testcontainers
this is my code
package com.app
import com.app.base.db.DbFactory
import com.app.features.auth.RegisterDTO
import com.app.plugins.*
import io.kotest.assertions.ktor.client.shouldHaveStatus
import io.kotest.core.extensions.install
import io.kotest.core.spec.style.FreeSpec
import io.kotest.extensions.testcontainers.JdbcTestContainerExtension
import io.ktor.client.*
import io.ktor.client.engine.apache.*
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.plugins.contentnegotiation.*
import org.testcontainers.containers.PostgreSQLContainer
import io.ktor.server.testing.*
import kotlinx.serialization.json.Json
import org.jetbrains.exposed.sql.Database
class AuthSpec : FreeSpec({
val postgres = PostgreSQLContainer<Nothing>("postgres").apply {
withDatabaseName("test_appDB")
startupAttempts = 1
withUsername("test_viktor")
withPassword("test_longPass")
withExposedPorts(5432)
}
postgres.start()
val ds = install(JdbcTestContainerExtension(postgres)) {
poolName = "myconnectionpool"
maximumPoolSize = 8
idleTimeout = 10000
}
"register creator" - {
testApplication {
application {
DbFactory.init(ds)
configureSecurity()
configureHTTP()
configureMonitoring()
configureSerialization()
configureDependencyInjection()
configureStatusPage()
configureRouting()
}
val client = HttpClient(Apache) {}
client.post("/api/v1/auth/register") {
contentType(ContentType.Application.Json)
setBody(RegisterDTO("some#gmail.com", "some", "some#sdfSDF"))
}.apply {
this.shouldHaveStatus(HttpStatusCode.OK)
}
}
}
})
I do expect that this test will run correctly and I can see the green in tests but actually i see the error
Fail to serialize body. Content has type: class com.app.features.auth.RegisterDTO, but OutgoingContent expected.
java.lang.IllegalStateException: Fail to serialize body. Content has type: class com.app.features.auth.RegisterDTO, but OutgoingContent expected.
If you expect serialized body, please check that you have installed the corresponding plugin(like `ContentNegotiation`) and set `Content-Type` header.
at io.ktor.client.plugins.HttpSend$Plugin$install$1.invokeSuspend(HttpSend.kt:87)
at io.ktor.client.plugins.HttpSend$Plugin$install$1.invoke(HttpSend.kt)
at io.ktor.client.plugins.HttpSend$Plugin$install$1.invoke(HttpSend.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:123)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:81)
at io.ktor.util.pipeline.SuspendFunctionGun.proceedWith(SuspendFunctionGun.kt:91)
at io.ktor.client.plugins.HttpCallValidator$Companion$install$1.invokeSuspend(HttpCallValidator.kt:126)
at io.ktor.client.plugins.HttpCallValidator$Companion$install$1.invoke(HttpCallValidator.kt)
at io.ktor.client.plugins.HttpCallValidator$Companion$install$1.invoke(HttpCallValidator.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:123)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:81)
at io.ktor.client.plugins.HttpRequestLifecycle$Plugin$install$1.invokeSuspend(HttpRequestLifecycle.kt:35)
at io.ktor.client.plugins.HttpRequestLifecycle$Plugin$install$1.invoke(HttpRequestLifecycle.kt)
at io.ktor.client.plugins.HttpRequestLifecycle$Plugin$install$1.invoke(HttpRequestLifecycle.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:123)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:81)
at io.ktor.util.pipeline.SuspendFunctionGun.execute$ktor_utils(SuspendFunctionGun.kt:101)
at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:77)
at io.ktor.client.HttpClient.execute$ktor_client_core(HttpClient.kt:187)
at io.ktor.client.statement.HttpStatement.executeUnsafe(HttpStatement.kt:107)
at io.ktor.client.statement.HttpStatement.execute(HttpStatement.kt:46)
at io.ktor.client.statement.HttpStatement.execute(HttpStatement.kt:61)
at com.app.AuthSpec$1$1$1.invokeSuspend(AuthSpec.kt:84)
i have installed a lot of libraries for tests probably they are not needed but here are they in dependendancies
//region tests
testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
testImplementation("io.ktor:ktor-server-test-host:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test:$kotlin_version")
testImplementation("io.kotest:kotest-runner-junit5:$kotest_version")
testImplementation("io.kotest:kotest-assertions-core:$kotest_version")
testImplementation("io.kotest:kotest-property:$kotest_version")
testImplementation("io.kotest.extensions:kotest-extensions-testcontainers:1.3.2")
testImplementation("org.testcontainers:testcontainers:$testcontainers_version")
testImplementation("org.testcontainers:junit-jupiter:$testcontainers_version")
testImplementation("org.testcontainers:postgresql:$testcontainers_version")
testImplementation("io.kotest.extensions:kotest-assertions-ktor:1.0.3")
//endregion
You need to install the ContentNegotiation plugin by configuring the test client.

I integrate an adminLTE template in Angular 8. the first time when I launch the server the program works correctly after it gives this error

ERROR in src/assets/plugins/filterizr/FilterContainer.d.ts:1:10 - error TS2305: Module '"D:/angular_workspace/templateAngular/src/assets/plugins/filterizr/FilterizrOptions/defaultOptions
"' has no exported member 'RawOptionsCallbacks'.
1 import { RawOptionsCallbacks } from './FilterizrOptions/defaultOptions';
~~~~~~~~~~~~~~~~~~~
src/assets/plugins/filterizr/FilterItems.d.ts:1:10 - error TS2305: Module '"D:/angular_workspace/templateAngular/src/assets/plugins/filterizr/ActiveFilter"' has no exported member 'Filte
r'.
1 import { Filter } from './ActiveFilter';
~~~~~~
src/assets/plugins/filterizr/Filterizr.d.ts:4:10 - error TS2305: Module '"D:/angular_workspace/templateAngular/src/assets/plugins/filterizr/ActiveFilter"' has no exported member 'Filter'
.
4 import { Filter } from './ActiveFilter';
~~~~~~
src/assets/plugins/filterizr/Filterizr.d.ts:5:10 - error TS2305: Module '"D:/angular_workspace/templateAngular/src/assets/plugins/filterizr/FilterizrOptions/defaultOptions"' has no expor
ted member 'RawOptions'.
5 import { RawOptions } from './FilterizrOptions/defaultOptions';
~~~~~~~~~~
** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
i 「wdm」: Failed to compile.
I got the same error when i tried to launch the server.
Replace imports in these classes:
"Filterizr.d.ts" in plugins/filterizr
Replace these imports:
import { Filter } from './ActiveFilter';
import { RawOptions } from './FilterizrOptions/defaultOptions';
For these:
import { Filter } from './types/index';
import { RawOptions } from './types/interfaces';
"FilterContainer.d.ts" in plugins/filterizr:
Replace this import:
import { RawOptionsCallbacks } from './FilterizrOptions/defaultOptions';
for:
import { RawOptionsCallbacks } from './types/interfaces';
"FilterItems.d.ts" in plugins/filterizr:
Replace this import:
import { Filter } from './ActiveFilter';
for
import { Filter } from './types';
Just remove the Filterizr folder from your plugins folder in assets
its useless for me and works
Filterizr plugin folder also contains the source code. Just keep the .js files and remove everything else inside /plugins/Filterizr folder. Remove .d.ts files. Problem solved.

Why is my Micronaut Controller bean not handled during tests

I am trying to use micronaut from Kotlin. I have this:
package me.test
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces
#Controller("/hello")
class Controller() {
#Get("/")
#Produces(MediaType.TEXT_PLAIN)
fun ping(): String {
return "hello world"
}
}
package me.test
import io.micronaut.runtime.Micronaut
object Application {
#JvmStatic
fun main(args: Array<String>) {
Micronaut.build()
.packages("me.test")
.mainClass(Application.javaClass)
.start()
}
}
I wrote the following controller test:
package me.test
import io.micronaut.http.client.RxHttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.annotation.MicronautTest
import org.junit.jupiter.api.Test
import javax.inject.Inject
#MicronautTest(application = Application::class)
class ControllerTest {
#Inject
#field:Client("/")
private lateinit var client: RxHttpClient
#Test
fun `should server ping with a pong`() {
val result = client.toBlocking().retrieve("/hello")
println(result)
}
}
but the test fails with a HttpClientResponseException: Page Not Found.
I have debugged this and from what I can tell, during the test, in DefaultBeanContext.getBeanDefinitions it doesn't find any beans for the #Controller qualifier. When I start the application using my Application class, I can see that it finds the Controller and makes the route available.
This is pretty much the Hello World for Micronaut, I am not sure what's going wrong here.
I think this is purely IDE related. I have Intellij set up to use Annotation Processors, to delegate builds to Gradle and to use the Gradle Test Runner. However, you also need to delete any existing test configurations and then the problem goes away.

Importing TypeScript Module located in path lower than current path throws Scope Error

In an attempt to put together an AMD-friendly TypeScript application skeleton, I've run into a snag: I can't seem to drop down from my current path to import a module in another directory. I can import modules that are above, but below throws an error:
TypeScript Error: The name ''../core/View'' does not exist in the current scope
Here the is the structure of my (very basic) app:
app/
- core/
- View.ts
- views/
- HomeView.ts
- Application.ts
In my Application.ts file, I can successfully import a module like so:
import HomeView = module( 'views/HomeView' );
export class Application {
constructor() {
console.log( 'initializing Application' );
}
}
Which, when using the --module AMD flag, correctly outputs
define(["require", "exports", 'views/HomeView'], function(require, exports, __HomeView__) {
var HomeView = __HomeView__;
var Application = (function () {
function Application() {
console.log('initializing Application', HomeView);
}
return Application;
})();
exports.Application = Application;
})
Now, the problem is when I jump into views/HomeView.js and attempt to import my core/View BaseClass to extend from:
import View = module('../core/View');
export class HomeView {
constructor() {
console.log('Hello HomeView!');
}
}
Which throws this complete error:
TypeScript Error: The name ''../core/View'' does not exist in the current scope
File: test/src/app/views/HomeView.ts
Start: 21, Length: 14
Line: import View = module('../core/View');
---------------------------^^^^^^^^^^^^^^--
Is this a bug in the compiler, or is my understanding of module imports faulty? Why would i be able to import views/HomeView, but not ../core/View?
Any help would be greatly appreciated.
I managed to get this to work using a root path - although I can't tell you why your relative path doesn't work.
import view = module("./app/core/View");

Grails 2.0.1: Spock test fail with "No bean named 'grailsLinkGenerator' is defined" where redirect is called

I recently upgraded from Grails 1.3.7 to Grails 2.0.1. The application runs normal, but I get into this trouble when cleaning up tests.
All my tests are spock tests. And I updated spock from 0.5-groovy-1.7 to 0.6 when upgrading.
All controller tests that will reach a line of redirect() fail in that line with the same error:
No bean named 'grailsLinkGenerator' is defined
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'grailsLinkGenerator' is defined
Any idea what cause this and how I could fix them?
This started happening for me in a controller unit test by extending spock's ControllerSpec. Switching it back to extend spock.lang.Specification fixed it.
The Spec:
package fileupload2
import static org.junit.Assert.assertThat
import static org.hamcrest.core.Is.is
import spock.lang.Specification;
import grails.test.mixin.TestFor
import grails.plugin.spock.ControllerSpec
#TestFor(FileUploadController)
//class FileUploadControllerSpec extends ControllerSpec {
class FileUploadControllerSpec extends Specification {
def "the index should redirect to create" () {
when:
controller.index()
then:
assertThat(response.redirectedUrl, is('/fileUpload/create'))
}
}
The CUT:
package fileupload2
class FileUploadController {
def fileUploadService
def index() {
redirect (action: "create")
}
def create() {
render(view: 'create')
}
def upload() {
...
}
}
I'm reluctant to say this is a bug as I'm about 2 weeks into this.
Extending UnitSpec seems to work also.