Skip to content

Commit

Permalink
Clear all mocks after each test
Browse files Browse the repository at this point in the history
  • Loading branch information
ursjoss committed Jun 12, 2020
1 parent 0c5b99c commit e98f02d
Show file tree
Hide file tree
Showing 24 changed files with 66 additions and 22 deletions.
1 change: 1 addition & 0 deletions common/common-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
}
api(Lib.junit5())
api(Lib.junit5("params"))
api(Lib.mockk())
api(Lib.kluent().id) {
exclude("org.mockito", "mockito-core")
exclude("com.nhaarman.mockitokotlin2", "mockito-kotlin")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ch.difty.scipamato.common

import io.mockk.clearAllMocks
import org.junit.jupiter.api.extension.AfterTestExecutionCallback
import org.junit.jupiter.api.extension.ExtensionContext

/**
* Junit extension that resets all mocks after every test execution.
* It should not be needed because MockkBean clears the mocks that have been created in the application context by
* default. Unfortunately, that doesn't work for nested tests due to a Spring Boot issue
* (see https://github.com/spring-projects/spring-boot/issues/12470). So, to make things easier, we keep using this
* extension.
* @author JB Nizet
*
* Taken from https://github.com/Ninja-Squad/springmockk/issues/12 - thanks to @jnizet
*/
class ClearAllMocksExtension : AfterTestExecutionCallback {
override fun afterTestExecution(context: ExtensionContext?) {
clearAllMocks()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.core.entity.search

import ch.difty.scipamato.common.ClearAllMocksExtension
import ch.difty.scipamato.common.entity.ScipamatoEntity.ScipamatoEntityFields.CREATED
import ch.difty.scipamato.common.entity.ScipamatoEntity.ScipamatoEntityFields.MODIFIED
import ch.difty.scipamato.core.entity.CoreEntity.CoreEntityFields.CREATOR_ID
Expand All @@ -21,7 +22,7 @@ import java.time.LocalDateTime

private const val SO_NAME = "soName"

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class SearchOrderTest {

private val so = SearchOrder(10L, SO_NAME, 1, false, null, null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.core.config

import ch.difty.scipamato.common.ClearAllMocksExtension
import ch.difty.scipamato.common.config.MavenProperties
import ch.difty.scipamato.core.logic.exporting.RisExporterStrategy
import ch.difty.scipamato.core.logic.parsing.AuthorParserStrategy
Expand All @@ -14,7 +15,7 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class ScipamatoCorePropertiesTest {

private lateinit var prop: ScipamatoCoreProperties
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.core.logic.export

import ch.difty.scipamato.common.ClearAllMocksExtension
import ch.difty.scipamato.core.config.ApplicationCoreProperties
import ch.difty.scipamato.core.logic.exporting.RisAdapterFactory
import ch.difty.scipamato.core.logic.exporting.RisExporterConfiguration
Expand All @@ -13,7 +14,7 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class RisExporterConfigurationTest {

private lateinit var conf: RisExporterConfiguration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.core.logic.parsing

import ch.difty.scipamato.common.ClearAllMocksExtension
import ch.difty.scipamato.core.config.ApplicationCoreProperties
import io.mockk.every
import io.mockk.impl.annotations.MockK
Expand All @@ -10,7 +11,7 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class AuthorParserConfigurationTest {

private lateinit var conf: AuthorParserConfiguration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.core.pubmed

import ch.difty.scipamato.common.ClearAllMocksExtension
import ch.difty.scipamato.core.config.ApplicationCoreProperties
import ch.difty.scipamato.core.persistence.PaperService
import ch.difty.scipamato.core.persistence.ServiceResult
Expand All @@ -17,7 +18,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import java.util.ArrayList

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class PubmedImportServiceTest {

private lateinit var pubmedImporter: PubmedImporter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.core.web

import ch.difty.scipamato.common.ClearAllMocksExtension
import ch.difty.scipamato.TestApplication
import ch.difty.scipamato.common.DateTimeService
import ch.difty.scipamato.common.navigator.ItemNavigator
Expand Down Expand Up @@ -32,7 +33,7 @@ import org.springframework.test.context.ActiveProfiles

@SpringBootTest
@ActiveProfiles("wickettest")
@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
abstract class AbstractWicketTest {

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package ch.difty.scipamato.core.web.common

import ch.difty.scipamato.common.ClearAllMocksExtension
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import org.amshove.kluent.shouldBeEqualTo
import org.apache.wicket.ajax.AjaxRequestTarget
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class SelfUpdateEventTest {

@MockK
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.core.web.paper

import ch.difty.scipamato.common.ClearAllMocksExtension
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import nl.jqno.equalsverifier.EqualsVerifier
Expand All @@ -9,7 +10,7 @@ import org.apache.wicket.ajax.AjaxRequestTarget
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class NewsletterChangeEventTest {

private lateinit var e: NewsletterChangeEvent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.core.web.paper

import ch.difty.scipamato.common.ClearAllMocksExtension
import ch.difty.scipamato.core.entity.PaperAttachment
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
Expand All @@ -13,7 +14,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import java.util.ArrayList

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class PaperAttachmentProviderTest {

private lateinit var provider: PaperAttachmentProvider
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.core.web.paper

import ch.difty.scipamato.common.ClearAllMocksExtension
import ch.difty.scipamato.core.entity.search.SearchCondition
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
Expand All @@ -13,7 +14,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import java.util.ArrayList

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class SearchConditionProviderTest {

private lateinit var provider: SearchConditionProvider
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.core.web.paper.common

import ch.difty.scipamato.common.ClearAllMocksExtension
import io.mockk.confirmVerified
import io.mockk.every
import io.mockk.impl.annotations.MockK
Expand All @@ -15,7 +16,7 @@ import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class NewFieldChangeEventTest {

private lateinit var e: NewFieldChangeEvent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.core.web.paper.search

import ch.difty.scipamato.common.ClearAllMocksExtension
import ch.difty.scipamato.core.web.paper.SearchOrderChangeEvent
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
Expand All @@ -13,7 +14,7 @@ import org.apache.wicket.ajax.AjaxRequestTarget
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class SearchOrderChangeEventTest {

private lateinit var e: SearchOrderChangeEvent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package ch.difty.scipamato.core.web.paper.search

import ch.difty.scipamato.common.ClearAllMocksExtension
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import org.amshove.kluent.shouldBeEqualTo
import org.apache.wicket.ajax.AjaxRequestTarget
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class ToggleExclusionsEventTest {

@MockK
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.core.web.security

import ch.difty.scipamato.common.ClearAllMocksExtension
import ch.difty.scipamato.core.entity.User
import ch.difty.scipamato.core.persistence.UserService
import ch.difty.scipamato.core.web.authentication.ScipamatoUserDetails
Expand All @@ -21,7 +22,7 @@ import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.security.core.userdetails.UsernameNotFoundException
import java.util.Optional

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class ScipamatoUserDetailsServiceTest {

private lateinit var service: ScipamatoUserDetailsService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.publ.persistence.paper

import ch.difty.scipamato.common.ClearAllMocksExtension
import ch.difty.scipamato.common.persistence.JooqSortMapper
import ch.difty.scipamato.publ.db.tables.Paper
import ch.difty.scipamato.publ.db.tables.records.PaperRecord
Expand All @@ -19,7 +20,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@Suppress("SpellCheckingInspection")
@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class JooqPublicPaperRepoTest {

@MockK
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.publ

import ch.difty.scipamato.common.ClearAllMocksExtension
import ch.difty.scipamato.common.logger
import ch.difty.scipamato.publ.config.ScipamatoPublicProperties
import io.mockk.every
Expand All @@ -10,7 +11,7 @@ import org.junit.jupiter.api.extension.ExtendWith

private val log = logger()

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class ScipamatoPublicApplicationTest {

@MockK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package ch.difty.scipamato.publ.config

import ch.difty.scipamato.common.ClearAllMocksExtension
import ch.difty.scipamato.common.config.MavenProperties
import io.mockk.confirmVerified
import io.mockk.every
Expand All @@ -14,7 +15,7 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class ScipamatoPublicPropertiesTest {

private lateinit var prop: ScipamatoPublicProperties
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.publ.misc

import ch.difty.scipamato.common.ClearAllMocksExtension
import ch.difty.scipamato.publ.config.ScipamatoPublicProperties
import io.mockk.every
import io.mockk.impl.annotations.MockK
Expand All @@ -10,7 +11,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import java.util.Locale

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class ParentUrlLocaleExtractorTest {

private lateinit var localeExtractor: LocaleExtractor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.publ.web.font

import ch.difty.scipamato.common.ClearAllMocksExtension
import ch.difty.scipamato.publ.config.ApplicationPublicProperties
import ch.difty.scipamato.publ.web.resources.IcoMoonCssResourceReference
import io.mockk.every
Expand All @@ -13,7 +14,7 @@ import org.amshove.kluent.shouldBeTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class IcoMoonFontResourceProviderTest {

private lateinit var provider: IcoMoonFontResourceProvider
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.publ.web.font

import ch.difty.scipamato.common.ClearAllMocksExtension
import ch.difty.scipamato.publ.config.ApplicationPublicProperties
import ch.difty.scipamato.publ.web.resources.MetaOTCssResourceReference
import io.mockk.every
Expand All @@ -13,7 +14,7 @@ import org.amshove.kluent.shouldBeTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class MetaOTFontResourceProviderTest {

private lateinit var provider: MetaOTFontResourceProvider
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.publ.web.font

import ch.difty.scipamato.common.ClearAllMocksExtension
import ch.difty.scipamato.publ.config.ApplicationPublicProperties
import ch.difty.scipamato.publ.web.resources.SimplonCssResourceReference
import io.mockk.every
Expand All @@ -13,7 +14,7 @@ import org.amshove.kluent.shouldBeTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class SimplonFontResourceProviderTest {

private lateinit var provider: SimplonFontResourceProvider
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.difty.scipamato.publ.web.paper.browse

import ch.difty.scipamato.common.ClearAllMocksExtension
import io.mockk.confirmVerified
import io.mockk.every
import io.mockk.impl.annotations.MockK
Expand All @@ -19,7 +20,7 @@ import org.junit.jupiter.api.extension.ExtendWith
private const val ID = "id"
private const val MARKUP_ID = "mId"

@ExtendWith(MockKExtension::class)
@ExtendWith(MockKExtension::class, ClearAllMocksExtension::class)
internal class SimpleFilterPanelChangeEventTest {

private lateinit var e: SimpleFilterPanelChangeEvent
Expand Down

0 comments on commit e98f02d

Please sign in to comment.