How can I share an ExternalResource between two classes in a junit test suite?
TestSuite.java
#RunWith(Suite.class)
#SuiteClasses({ TestA.class, TestB.class })
public class TestSuite {
#ClassRule
public static MyResource res = new MyResource();
}
MyResource is an ExternalResource and I want to access res in the test suite classes TestA and TestB.
TestA.java
public class TestA {
#Test
public void testA() {
System.out.println("####### testA");
// res.someGetMethod();
}
}
TestB.java
public class TestB {
#Test
public void testB() {
System.out.println("####### testB");
// res.someGetMethod();
}
}
Thanks for looking.
The ClassRules of the Suite are shared and accessible from its TestCases, it will intialised before the test case, and hence you can access it as you do with any public static variable
#Test
public void testA() {
System.out.println("####### testA");
TestSuite.res.someGetMethod();
}
Related
So I am trying to run JUnit parameterized tests along with non-parameterized tests in the same test class. But I am running into one error or the other. Has anyone tried this before and were they successful in doing so? I know other runners need to be used with the #PowerMockRunnerDelegate in order to run correctly. So here's what I came up with:
#RunWith(PowerMockRunner.class)
#PowerMockRunnerDelegate(Enclosed.class)
#PrepareForTest(Some.class)
#PowerMockIgnore("javax.management.*")
public class TestClass {
#PowerMockRunnerDelegate(Parameterized.class)
public static class ParameterizedTests {
}
#Test
public void nonParameterizedTestOne() {
}
#Test
public void nonParameterizedTestTwo() {
}
}
But I get the error:
Test class should have exactly one public zero-argument constructor
Without powermock, this situation can be easily handled with:
#RunWith(Enclosed.class)
public class TestClass {
#RunWith(Parameterized.class)
public static class ParameterizedTests {
}
#Test
public void nonParameterizedTestOne() {
}
#Test
public void nonParameterizedTestTwo() {
}
}
But I would definitely like to use powermock. Any solutions?
I had the same issue, this worked for me:
#RunWith(PowerMockRunner.class)
#PowerMockRunnerDelegate(Enclosed.class)
#PrepareForTest({Some.class})
public class TestClass {
private static void setUpOnceForAllTests() {
}
private static void setUpForEveryTest() {
}
public static class SingleTests {
// Setup once for all single tests
#BeforeClass
public static void setUpBeforeClass() {
setUpOnceForTests();
}
// Setup for each and every single test
#Before
public void setUp() {
setUpForEveryTest();
}
#Test
public void nonParameterizedTestOne() {
}
#Test
public void nonParameterizedTestTwo() {
}
}
#PowerMockRunnerDelegate(Parameterized.class)
public static class ParameterizedTests {
// Setup once for all parameterized test
#BeforeClass
public static void setUpBeforeClass() {
setUpOnceForTests();
}
// Setup for each and every parameterized test
#Before
public void setUp() {
setUpForEveryTest();
}
#Parameterized.Parameters
public static Collection<Enum> param() {
return new ArrayList<>(Arrays.asList(Enum.values()));
}
#Parameterized.Parameter
public int param;
#Test
public void parameterizedTestOne() {
}
#Test
public void parameterizedTestTwo() {
}
}
}
I have grouped my test methods in 2 Test Groups like so -
#TestGroup("integration")
public abstract static class AbstractIntegrationTest {
#ClassRule
public static TestGroupRule rule = new TestGroupRule();
}
#TestGroup("unit")
public abstract static class AbstractUnitTest {
#ClassRule
public static TestGroupRule rule = new TestGroupRule();
}
And have my test methods defined this way -
public static class UnitTest extends AbstractUnitTest {
#Test
public void Test1() {...}
#Test
public void Test2() {...}
}
public static class IntegrationTest extends AbstractIntegrationTest {
#Test
public void Test3() {...}
#Test
public void Test4() {...}
}
When I try to run one of my test methods (say Test1) from the Intellij IDE, I get an error saying -
org.junit.AssumptionViolatedException: None of the test groups [unit] are enabled. Enabled test groups: []
How can I enable my test group and if I am running a specific test, do I need to still enable anything?Have I set this up completely wrong?
Thanks.
I've seen how to unit test classes that use Utility classes by mocking the Static method, but I haven't been able to figure out how to unit test the actual Utility Class.
Here is the Utility Class
public class DbNameContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static void setDbName(String dbName){
contextHolder.set(dbName);
}
public static String getDbName(){
return (String) contextHolder.get();
}
public static void clearDbName(){
contextHolder.remove();
}
}
Here is what I've tried so far for a unit test
#RunWith(PowerMockRunner.class)
#PrepareForTest({DbNameContextHolder.class, ThreadLocal.class})
public class DbNameContextHolderTest {
#SuppressWarnings("rawtypes")
#Mock
ThreadLocal threadLocalMock;
#Before
public void init() throws Exception{
PowerMockito.whenNew(ThreadLocal.class).withNoArguments().thenReturn(threadLocalMock);
}
#Test
public void setsDBName(){
DbNameContextHolder.setDbName("someName");
verify(threadLocalMock).set("someName");
}
#Test
public void getsDbName(){
DbNameContextHolder.getDbName();
verify(threadLocalMock).get();
}
#Test
public void clearsDBName(){
DbNameContextHolder.clearDbName();
verify(threadLocalMock).remove();
}
}
How do I mock a utility class like this?
Using the suggestions in the comments I've tested the expected outcome.
#RunWith(MockitoJUnitRunner.class)
public class DbNameContextHolderTest {
#Test
public void setsAndGetsDBNameCorrectly(){
DbNameContextHolder.setDbName("someName");
String returnedName = DbNameContextHolder.getDbName();
assertEquals("someName",returnedName);
}
#Test
public void clearsDBName(){
DbNameContextHolder.setDbName("someName");
String returnedName = DbNameContextHolder.getDbName();
assertEquals("someName",returnedName);
DbNameContextHolder.clearDbName();
returnedName = DbNameContextHolder.getDbName();
assertNull(returnedName);
}
}
I have TestNG tests in some classes, which extends the same main class.
I need the same last test for class1 and class2, but if I add #Test(priority = 666) to main class it start after all classes.
How I should annotate #Test in main class what would it starts after all tests of each class?
Big thanks. Also sorry for bad english.
main class
public class MainTest {
#BeforeClass()
public void setup() {
//something
}
#AfterClass()
public void tearDown() {
//something
}
#AfterMethod()
public void log_writer(Method method) {
//something
}
#Test(priority = 666) {}
}
class1
public class Class1 extends MainTest {
#Test
public void test1(){}
#Test
public void test2(){}
}
and class2
public class Class2 extends MainTest {
#Test
public void test1(){}
#Test
public void test2(){}
}
what you are looking for is the #AfterClass annotation. handle the part where you want to check logs in the AfterClass annotated method
It's not possible. Basically each #Test runs only once. If you need to do something after each test class you have to use #AfterClass annotation on method in your MainTest. You can do some hacks with method order etc. in MethodInterceptor (http://testng.org/doc/documentation-main.html#methodinterceptors) but it's not good idea for this case.
Is it possible to place the setup/teardown methods using JUnit framework in a single class (which would be my baseclass) so on test runs they methods are always called first/last? it would be in a similar way to which nunit tests can be structured
currently the only way I can get my tests to kick off is if I have the setup/teardown methods within the same class as my tests are (which is something I wan't to avoid, to keep my test classes tidy)
example I would hope to set up;
public class baseclass
{
#Before
public void setUp
{}
#After
public void tearDown
{}
}
public class tests
{
#Test
public void test1
{
// test content here
}
}
Run this test and see the sequence of events
class Test1 {
#Before
public void setUp1() {
System.out.println("setUp1");
}
}
public class Test2 extends Test1 {
#Before
public void setUp2() {
System.out.println("setUp2");
}
#Test
public void test() {
System.out.println("test");
}
}
Yes, as long as your test class extend your baseclass.
For instance:
Suite
#RunWith(Suite.class)
#SuiteClasses(Tests.class)
public class AllTests {
}
BaseClass
public class BaseClass {
#BeforeClass
public static void beforeAll() {
}
#Before
public void setUp() {
}
#After
public void tearDown {
}
#AfterClass
public static void afterAll() {
}
}
Tests
public class Test extends BaseClass {
#Test
public void test1() {
}
#Test
public void test2() {
}
}