Search in sources :

Example 1 with TestInstanceFactory

use of org.junit.jupiter.api.extension.TestInstanceFactory in project junit5 by junit-team.

the class TestInstanceFactoryTests method nullTestInstanceFactoryWithPerMethodLifecycle.

@Test
void nullTestInstanceFactoryWithPerMethodLifecycle() {
    Class<?> testClass = NullTestInstanceFactoryTestCase.class;
    EngineExecutionResults executionResults = executeTestsForClass(testClass);
    assertEquals(1, executionResults.testEvents().started().count(), "# tests started");
    assertEquals(1, executionResults.testEvents().failed().count(), "# tests aborted");
    // 
    executionResults.allEvents().assertEventsMatchExactly(// 
    event(engine(), started()), // 
    event(container(testClass), started()), // 
    event(test("testShouldNotBeCalled"), started()), event(test("testShouldNotBeCalled"), finishedWithFailure(instanceOf(TestInstantiationException.class), message(m -> m.equals("TestInstanceFactory [" + NullTestInstanceFactory.class.getName() + "] failed to return an instance of [" + testClass.getName() + // 
    "] and instead returned an instance of [null].")))), // 
    event(container(testClass), finishedSuccessfully()), event(engine(), finishedSuccessfully()));
}
Also used : EngineExecutionResults(org.junit.platform.testkit.engine.EngineExecutionResults) BeforeEach(org.junit.jupiter.api.BeforeEach) EventConditions.event(org.junit.platform.testkit.engine.EventConditions.event) EventConditions.nestedContainer(org.junit.platform.testkit.engine.EventConditions.nestedContainer) URL(java.net.URL) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EventConditions.finishedWithFailure(org.junit.platform.testkit.engine.EventConditions.finishedWithFailure) ClassUtils.nullSafeToString(org.junit.platform.commons.util.ClassUtils.nullSafeToString) EventConditions.finishedSuccessfully(org.junit.platform.testkit.engine.EventConditions.finishedSuccessfully) TestExecutionResultConditions.message(org.junit.platform.testkit.engine.TestExecutionResultConditions.message) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Nested(org.junit.jupiter.api.Nested) ArrayList(java.util.ArrayList) EventConditions.container(org.junit.platform.testkit.engine.EventConditions.container) AfterAll(org.junit.jupiter.api.AfterAll) URLClassLoader(java.net.URLClassLoader) TestInstance(org.junit.jupiter.api.TestInstance) EventConditions.started(org.junit.platform.testkit.engine.EventConditions.started) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeAll(org.junit.jupiter.api.BeforeAll) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) AbstractJupiterTestEngineTests(org.junit.jupiter.engine.AbstractJupiterTestEngineTests) EventConditions.test(org.junit.platform.testkit.engine.EventConditions.test) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EngineExecutionResults(org.junit.platform.testkit.engine.EngineExecutionResults) TestInstanceFactoryContext(org.junit.jupiter.api.extension.TestInstanceFactoryContext) TestInstantiationException(org.junit.jupiter.api.extension.TestInstantiationException) PER_CLASS(org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS) TestInstanceFactory(org.junit.jupiter.api.extension.TestInstanceFactory) ExtensionConfigurationException(org.junit.jupiter.api.extension.ExtensionConfigurationException) ReflectionUtils(org.junit.platform.commons.util.ReflectionUtils) EventConditions.engine(org.junit.platform.testkit.engine.EventConditions.engine) Test(org.junit.jupiter.api.Test) List(java.util.List) TestExecutionResultConditions.instanceOf(org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf) TestInstantiationException(org.junit.jupiter.api.extension.TestInstantiationException) Test(org.junit.jupiter.api.Test)

Example 2 with TestInstanceFactory

use of org.junit.jupiter.api.extension.TestInstanceFactory in project junit5 by junit-team.

the class TestInstanceFactoryTests method proxyTestInstanceFactoryFailsDueToUseOfDifferentClassLoader.

@Test
void proxyTestInstanceFactoryFailsDueToUseOfDifferentClassLoader() {
    Class<?> testClass = ProxiedTestCase.class;
    EngineExecutionResults executionResults = executeTestsForClass(testClass);
    assertEquals(0, executionResults.testEvents().started().count(), "# tests started");
    assertEquals(0, executionResults.testEvents().failed().count(), "# tests aborted");
    // 
    executionResults.allEvents().assertEventsMatchExactly(// 
    event(engine(), started()), // 
    event(container(testClass), started()), event(// 
    container(testClass), // loaded by the different ClassLoader.
    finishedWithFailure(instanceOf(TestInstantiationException.class), message(m -> m.startsWith("TestInstanceFactory [" + ProxyTestInstanceFactory.class.getName() + "]") && m.contains("failed to return an instance of [" + testClass.getName() + "@" + Integer.toHexString(System.identityHashCode(testClass))) && // 
    m.contains("and instead returned an instance of [" + testClass.getName() + "@")))), event(engine(), finishedSuccessfully()));
}
Also used : EngineExecutionResults(org.junit.platform.testkit.engine.EngineExecutionResults) BeforeEach(org.junit.jupiter.api.BeforeEach) EventConditions.event(org.junit.platform.testkit.engine.EventConditions.event) EventConditions.nestedContainer(org.junit.platform.testkit.engine.EventConditions.nestedContainer) URL(java.net.URL) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EventConditions.finishedWithFailure(org.junit.platform.testkit.engine.EventConditions.finishedWithFailure) ClassUtils.nullSafeToString(org.junit.platform.commons.util.ClassUtils.nullSafeToString) EventConditions.finishedSuccessfully(org.junit.platform.testkit.engine.EventConditions.finishedSuccessfully) TestExecutionResultConditions.message(org.junit.platform.testkit.engine.TestExecutionResultConditions.message) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Nested(org.junit.jupiter.api.Nested) ArrayList(java.util.ArrayList) EventConditions.container(org.junit.platform.testkit.engine.EventConditions.container) AfterAll(org.junit.jupiter.api.AfterAll) URLClassLoader(java.net.URLClassLoader) TestInstance(org.junit.jupiter.api.TestInstance) EventConditions.started(org.junit.platform.testkit.engine.EventConditions.started) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeAll(org.junit.jupiter.api.BeforeAll) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) AbstractJupiterTestEngineTests(org.junit.jupiter.engine.AbstractJupiterTestEngineTests) EventConditions.test(org.junit.platform.testkit.engine.EventConditions.test) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EngineExecutionResults(org.junit.platform.testkit.engine.EngineExecutionResults) TestInstanceFactoryContext(org.junit.jupiter.api.extension.TestInstanceFactoryContext) TestInstantiationException(org.junit.jupiter.api.extension.TestInstantiationException) PER_CLASS(org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS) TestInstanceFactory(org.junit.jupiter.api.extension.TestInstanceFactory) ExtensionConfigurationException(org.junit.jupiter.api.extension.ExtensionConfigurationException) ReflectionUtils(org.junit.platform.commons.util.ReflectionUtils) EventConditions.engine(org.junit.platform.testkit.engine.EventConditions.engine) Test(org.junit.jupiter.api.Test) List(java.util.List) TestExecutionResultConditions.instanceOf(org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf) TestInstantiationException(org.junit.jupiter.api.extension.TestInstantiationException) Test(org.junit.jupiter.api.Test)

Example 3 with TestInstanceFactory

use of org.junit.jupiter.api.extension.TestInstanceFactory in project junit5 by junit-team.

the class TestInstanceFactoryTests method nullTestInstanceFactoryWithPerClassLifecycle.

@Test
void nullTestInstanceFactoryWithPerClassLifecycle() {
    Class<?> testClass = PerClassLifecycleNullTestInstanceFactoryTestCase.class;
    EngineExecutionResults executionResults = executeTestsForClass(testClass);
    assertEquals(0, executionResults.testEvents().started().count(), "# tests started");
    assertEquals(0, executionResults.testEvents().failed().count(), "# tests aborted");
    // 
    executionResults.allEvents().assertEventsMatchExactly(// 
    event(engine(), started()), // 
    event(container(testClass), started()), event(container(testClass), finishedWithFailure(instanceOf(TestInstantiationException.class), message(m -> m.equals("TestInstanceFactory [" + NullTestInstanceFactory.class.getName() + "] failed to return an instance of [" + testClass.getName() + // 
    "] and instead returned an instance of [null].")))), event(engine(), finishedSuccessfully()));
}
Also used : EngineExecutionResults(org.junit.platform.testkit.engine.EngineExecutionResults) BeforeEach(org.junit.jupiter.api.BeforeEach) EventConditions.event(org.junit.platform.testkit.engine.EventConditions.event) EventConditions.nestedContainer(org.junit.platform.testkit.engine.EventConditions.nestedContainer) URL(java.net.URL) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EventConditions.finishedWithFailure(org.junit.platform.testkit.engine.EventConditions.finishedWithFailure) ClassUtils.nullSafeToString(org.junit.platform.commons.util.ClassUtils.nullSafeToString) EventConditions.finishedSuccessfully(org.junit.platform.testkit.engine.EventConditions.finishedSuccessfully) TestExecutionResultConditions.message(org.junit.platform.testkit.engine.TestExecutionResultConditions.message) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Nested(org.junit.jupiter.api.Nested) ArrayList(java.util.ArrayList) EventConditions.container(org.junit.platform.testkit.engine.EventConditions.container) AfterAll(org.junit.jupiter.api.AfterAll) URLClassLoader(java.net.URLClassLoader) TestInstance(org.junit.jupiter.api.TestInstance) EventConditions.started(org.junit.platform.testkit.engine.EventConditions.started) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeAll(org.junit.jupiter.api.BeforeAll) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) AbstractJupiterTestEngineTests(org.junit.jupiter.engine.AbstractJupiterTestEngineTests) EventConditions.test(org.junit.platform.testkit.engine.EventConditions.test) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EngineExecutionResults(org.junit.platform.testkit.engine.EngineExecutionResults) TestInstanceFactoryContext(org.junit.jupiter.api.extension.TestInstanceFactoryContext) TestInstantiationException(org.junit.jupiter.api.extension.TestInstantiationException) PER_CLASS(org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS) TestInstanceFactory(org.junit.jupiter.api.extension.TestInstanceFactory) ExtensionConfigurationException(org.junit.jupiter.api.extension.ExtensionConfigurationException) ReflectionUtils(org.junit.platform.commons.util.ReflectionUtils) EventConditions.engine(org.junit.platform.testkit.engine.EventConditions.engine) Test(org.junit.jupiter.api.Test) List(java.util.List) TestExecutionResultConditions.instanceOf(org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf) TestInstantiationException(org.junit.jupiter.api.extension.TestInstantiationException) Test(org.junit.jupiter.api.Test)

Example 4 with TestInstanceFactory

use of org.junit.jupiter.api.extension.TestInstanceFactory in project junit5 by junit-team.

the class TestInstanceFactoryTests method bogusTestInstanceFactoryWithPerClassLifecycle.

@Test
void bogusTestInstanceFactoryWithPerClassLifecycle() {
    Class<?> testClass = PerClassLifecycleBogusTestInstanceFactoryTestCase.class;
    EngineExecutionResults executionResults = executeTestsForClass(testClass);
    assertEquals(0, executionResults.testEvents().started().count(), "# tests started");
    assertEquals(0, executionResults.testEvents().failed().count(), "# tests aborted");
    // 
    executionResults.allEvents().assertEventsMatchExactly(// 
    event(engine(), started()), // 
    event(container(testClass), started()), event(container(testClass), finishedWithFailure(instanceOf(TestInstantiationException.class), message(m -> m.equals("TestInstanceFactory [" + BogusTestInstanceFactory.class.getName() + "] failed to return an instance of [" + testClass.getName() + // 
    "] and instead returned an instance of [java.lang.String].")))), event(engine(), finishedSuccessfully()));
}
Also used : EngineExecutionResults(org.junit.platform.testkit.engine.EngineExecutionResults) BeforeEach(org.junit.jupiter.api.BeforeEach) EventConditions.event(org.junit.platform.testkit.engine.EventConditions.event) EventConditions.nestedContainer(org.junit.platform.testkit.engine.EventConditions.nestedContainer) URL(java.net.URL) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EventConditions.finishedWithFailure(org.junit.platform.testkit.engine.EventConditions.finishedWithFailure) ClassUtils.nullSafeToString(org.junit.platform.commons.util.ClassUtils.nullSafeToString) EventConditions.finishedSuccessfully(org.junit.platform.testkit.engine.EventConditions.finishedSuccessfully) TestExecutionResultConditions.message(org.junit.platform.testkit.engine.TestExecutionResultConditions.message) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Nested(org.junit.jupiter.api.Nested) ArrayList(java.util.ArrayList) EventConditions.container(org.junit.platform.testkit.engine.EventConditions.container) AfterAll(org.junit.jupiter.api.AfterAll) URLClassLoader(java.net.URLClassLoader) TestInstance(org.junit.jupiter.api.TestInstance) EventConditions.started(org.junit.platform.testkit.engine.EventConditions.started) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeAll(org.junit.jupiter.api.BeforeAll) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) AbstractJupiterTestEngineTests(org.junit.jupiter.engine.AbstractJupiterTestEngineTests) EventConditions.test(org.junit.platform.testkit.engine.EventConditions.test) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EngineExecutionResults(org.junit.platform.testkit.engine.EngineExecutionResults) TestInstanceFactoryContext(org.junit.jupiter.api.extension.TestInstanceFactoryContext) TestInstantiationException(org.junit.jupiter.api.extension.TestInstantiationException) PER_CLASS(org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS) TestInstanceFactory(org.junit.jupiter.api.extension.TestInstanceFactory) ExtensionConfigurationException(org.junit.jupiter.api.extension.ExtensionConfigurationException) ReflectionUtils(org.junit.platform.commons.util.ReflectionUtils) EventConditions.engine(org.junit.platform.testkit.engine.EventConditions.engine) Test(org.junit.jupiter.api.Test) List(java.util.List) TestExecutionResultConditions.instanceOf(org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf) TestInstantiationException(org.junit.jupiter.api.extension.TestInstantiationException) Test(org.junit.jupiter.api.Test)

Example 5 with TestInstanceFactory

use of org.junit.jupiter.api.extension.TestInstanceFactory in project junit5 by junit-team.

the class TestInstanceFactoryTests method bogusTestInstanceFactoryWithPerMethodLifecycle.

@Test
void bogusTestInstanceFactoryWithPerMethodLifecycle() {
    Class<?> testClass = BogusTestInstanceFactoryTestCase.class;
    EngineExecutionResults executionResults = executeTestsForClass(testClass);
    assertEquals(1, executionResults.testEvents().started().count(), "# tests started");
    assertEquals(1, executionResults.testEvents().failed().count(), "# tests aborted");
    // 
    executionResults.allEvents().assertEventsMatchExactly(// 
    event(engine(), started()), // 
    event(container(testClass), started()), // 
    event(test("testShouldNotBeCalled"), started()), event(test("testShouldNotBeCalled"), finishedWithFailure(instanceOf(TestInstantiationException.class), message(m -> m.equals("TestInstanceFactory [" + BogusTestInstanceFactory.class.getName() + "] failed to return an instance of [" + testClass.getName() + // 
    "] and instead returned an instance of [java.lang.String].")))), // 
    event(container(testClass), finishedSuccessfully()), event(engine(), finishedSuccessfully()));
}
Also used : EngineExecutionResults(org.junit.platform.testkit.engine.EngineExecutionResults) BeforeEach(org.junit.jupiter.api.BeforeEach) EventConditions.event(org.junit.platform.testkit.engine.EventConditions.event) EventConditions.nestedContainer(org.junit.platform.testkit.engine.EventConditions.nestedContainer) URL(java.net.URL) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EventConditions.finishedWithFailure(org.junit.platform.testkit.engine.EventConditions.finishedWithFailure) ClassUtils.nullSafeToString(org.junit.platform.commons.util.ClassUtils.nullSafeToString) EventConditions.finishedSuccessfully(org.junit.platform.testkit.engine.EventConditions.finishedSuccessfully) TestExecutionResultConditions.message(org.junit.platform.testkit.engine.TestExecutionResultConditions.message) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Nested(org.junit.jupiter.api.Nested) ArrayList(java.util.ArrayList) EventConditions.container(org.junit.platform.testkit.engine.EventConditions.container) AfterAll(org.junit.jupiter.api.AfterAll) URLClassLoader(java.net.URLClassLoader) TestInstance(org.junit.jupiter.api.TestInstance) EventConditions.started(org.junit.platform.testkit.engine.EventConditions.started) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeAll(org.junit.jupiter.api.BeforeAll) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) AbstractJupiterTestEngineTests(org.junit.jupiter.engine.AbstractJupiterTestEngineTests) EventConditions.test(org.junit.platform.testkit.engine.EventConditions.test) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EngineExecutionResults(org.junit.platform.testkit.engine.EngineExecutionResults) TestInstanceFactoryContext(org.junit.jupiter.api.extension.TestInstanceFactoryContext) TestInstantiationException(org.junit.jupiter.api.extension.TestInstantiationException) PER_CLASS(org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS) TestInstanceFactory(org.junit.jupiter.api.extension.TestInstanceFactory) ExtensionConfigurationException(org.junit.jupiter.api.extension.ExtensionConfigurationException) ReflectionUtils(org.junit.platform.commons.util.ReflectionUtils) EventConditions.engine(org.junit.platform.testkit.engine.EventConditions.engine) Test(org.junit.jupiter.api.Test) List(java.util.List) TestExecutionResultConditions.instanceOf(org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf) TestInstantiationException(org.junit.jupiter.api.extension.TestInstantiationException) Test(org.junit.jupiter.api.Test)

Aggregations

URL (java.net.URL)5 URLClassLoader (java.net.URLClassLoader)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 AfterAll (org.junit.jupiter.api.AfterAll)5 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)5 BeforeAll (org.junit.jupiter.api.BeforeAll)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Nested (org.junit.jupiter.api.Nested)5 Test (org.junit.jupiter.api.Test)5 TestInstance (org.junit.jupiter.api.TestInstance)5 PER_CLASS (org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS)5 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)5 ExtensionConfigurationException (org.junit.jupiter.api.extension.ExtensionConfigurationException)5 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)5 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)5 TestInstanceFactory (org.junit.jupiter.api.extension.TestInstanceFactory)5 TestInstanceFactoryContext (org.junit.jupiter.api.extension.TestInstanceFactoryContext)5