From d3073876b202fcaac1ef776afd4ea0d834ec6a5a Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 28 May 2020 13:17:34 +0200 Subject: [PATCH] testframework example "ctxmembers" --- .../framework/examples/ctxmembers/Makefile | 25 +++++++ .../examples/ctxmembers/TestMain.java | 65 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 test/java/foundation/pEp/jniadapter/test/framework/examples/ctxmembers/Makefile create mode 100644 test/java/foundation/pEp/jniadapter/test/framework/examples/ctxmembers/TestMain.java diff --git a/test/java/foundation/pEp/jniadapter/test/framework/examples/ctxmembers/Makefile b/test/java/foundation/pEp/jniadapter/test/framework/examples/ctxmembers/Makefile new file mode 100644 index 0000000..b151a65 --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/framework/examples/ctxmembers/Makefile @@ -0,0 +1,25 @@ +include ../Makefile.conf + +TEST_UNIT_NAME=ctxmembers + +JAVA_CLASSES = \ + TestMain.class \ + +# Use the test framework +JAVA_CLASSES += $(JAVA_CLASSES_FRAMEWORK) + +.PHONY: compile run test clean + +all: compile + $(MAKE) run + +run: compile + cd $(JAVA_CWD);$(JAVA) $(JAVA_PKG_BASENAME).$(TEST_UNIT_NAME).TestMain + +compile: $(JAVA_CLASSES) + +%.class: %.java + cd $(JAVA_CWD);pwd;javac $(JAVA_PKG_BASEPATH)/$(TEST_UNIT_NAME)/$< + +clean: + rm -f $(JAVA_CLASSES) diff --git a/test/java/foundation/pEp/jniadapter/test/framework/examples/ctxmembers/TestMain.java b/test/java/foundation/pEp/jniadapter/test/framework/examples/ctxmembers/TestMain.java new file mode 100644 index 0000000..24786c1 --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/framework/examples/ctxmembers/TestMain.java @@ -0,0 +1,65 @@ +package foundation.pEp.jniadapter.test.framework.examples.ctxmembers; +import static foundation.pEp.jniadapter.test.framework.TestLogger.*; +import foundation.pEp.jniadapter.test.framework.*; + + +// Context member object instantiation +// +// In the Context, do not use member objects like this: +// A a = new A(); // WRONG +// Always, declare them as members and instantiate them in the constructor +// Otherwise, all test contexts in a program partially execute before the test using it is actually being run +// Context init() is part of the test + +class HelloWorldTestContext extends AbstractTestContext { + String name; + ExampleCtxMember correct; + ExampleCtxMember incorrect = new ExampleCtxMember(false); // WRONG + + @Override + public void init() throws Throwable { + log("HelloWorldTestContext: init() called"); + correct = new ExampleCtxMember(true); + name = "UnitTestFrameWorkWithoutAName"; + } +} + +class TestMain { + public static void main(String[] args) throws Exception { + new TestUnit("Hello World",new HelloWorldTestContext() , ctx -> { + // do stuff using the context + // Test FAILS on unhandled exception, otherwise SUCCESS + log("Hello World from: " + ctx.name); + }).run(); + } +} + + + + + + + + + + + + + + + + + + + + +// Just an example member obj +class ExampleCtxMember { + ExampleCtxMember(boolean correct){ + if(correct) { + log("Hya from member obj used correctly"); + } else { + log("Hya from member obj used wrong"); + } + } +}