Artglorin
04/04/2019, 2:12 PMimport static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class SpringJavaConfiguration {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext context;
@BeforeEach
void setUp() {
mockMvc = webAppContextSetup(context)
.apply(springSecurity())
.build();
}
}
Then I try to do same on Kotlin
import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity
import org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup
@ExtendWith(SpringExtension::class)
@SpringBootTest
class SpringKotlinConfiguration {
private var mockMvc: MockMvc? = null
@Autowired
lateinit var context: WebApplicationContext
@BeforeEach
internal fun setUp() {
mockMvc = webAppContextSetup(context)
.apply(springSecurity()) // cannot inference type here
.build()
}
}
As you can see result of call method apply cannot be inferred.
Signatures of class and method apply
present below
public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
extends MockMvcBuilderSupport implements ConfigurableMockMvcBuilder<B> {
public final <T extends B> T apply(MockMvcConfigurer configurer) {
//method code
}
And I cannot specify type myself because it is cyclic dependency on itself either suppress it.
You can find source code here: https://github.com/artglorin/kotlin-type-inference-failureribesg
04/04/2019, 2:33 PMArtglorin
04/04/2019, 2:36 PMhho
04/04/2019, 2:39 PMwebAppContextSetup(…)
returns a DefaultMockMvcBuilder
, I'd just use that here.apply<DefaultMockMvcBuilder>(springSecurity())
Artglorin
04/04/2019, 2:43 PM