You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@calcite.apache.org by Stamatis Zampetakis <za...@gmail.com> on 2019/12/02 12:53:10 UTC

JUnit 4 and JUnit 5 annotations do not mix well

Hello,

At the moment, we can use both JUnit 4 and 5 annotations in our code base.
Lately, I spend some time on debugging a few test cases only to realize
that old and new APIs do not mix very well. If you encounter problems with
your annotations then consider checking your imports. The problem might lie
on the fact that you are using at the same time annotations from different
versions.

Consider for example the following test.

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Disabled;

public class SimpleTest {

  @Test
  @Disabled
  public void test() {
    Assert.fail("Should not run because it is disabled");
  }
}

Contrary to what people might expect the test above will run and will fail
with an assertion error. Changing the imports (see below) will make the
test behave as expected.

package org.apache.calcite;

import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Disabled;

public class SimpleTest {

  @Test
  @Disabled
  public void test() {
    Assert.fail("Should not run because it is disabled");
  }
}

Best,
Stamatis

Re: JUnit 4 and JUnit 5 annotations do not mix well

Posted by Rui Wang <am...@apache.org>.
+1 on merging #1621 soon. It's a large change so could easily occur code
conflict.


-Rui

On Tue, Dec 3, 2019 at 10:42 AM Andrei Sereda <an...@sereda.cc> wrote:

> Thanks Vladimir. Most of the changes look find to me (left a small comment
> in PR)
>
> Once merged, I'll take care of remaining JUnit4 rules for
> Cassandra/ElasticSearch/Mongo etc.
>
>
> On Tue, Dec 3, 2019 at 1:23 PM Vladimir Sitnikov <
> sitnikov.vladimir@gmail.com> wrote:
>
> > Ok, I think PR1621 is ready to go:
> > https://github.com/apache/calcite/pull/1621
> > I'm inclined to merge it shortly
> >
> > The PR converts most of the tests, and by default, it allows JUnit5 only.
> >
> > Certain modules still have "non-trivial" JUnit4 code, and there was an
> > agreement to convert them later.
> > The modules receive junit4 dependency via junit4=true in
> gradle.properties.
> >
> > ---
> >
> > The most "controversial" change from my point of view is the removal of
> > *Suite* classes.
> > In other words, I have dropped classes like
> > CalciteSuite, FileSuite, PlusSuite, and so on.
> >
> > I guess it is OK, as both Gradle and JUnit5 provide lots of ways to
> launch
> > tests (e.g. tags are good).
> > The good thing about the removal is it would reduce the number of
> > merge-conflicts,
> > and it would simplify adding new test classes.
> >
> > ---
> >
> > The most notable difference between junit4 and junit5 .assert* is that
> > junit4 has (message, expected, actual)
> > argument order while junit5 uses (expected, actual, message).
> > The latter is better when message is big (e.g. concatenated), and junit5
> > allows to use lambda,
> > so the message is built only when assertion fails.
> >
> > I've performed argument shuffling with IntelliJ IDEA's "structural search
> > and replace", so it should be OK.
> >
> > Vladimir
> >
>

Re: JUnit 4 and JUnit 5 annotations do not mix well

Posted by Andrei Sereda <an...@sereda.cc>.
Thanks Vladimir. Most of the changes look find to me (left a small comment
in PR)

Once merged, I'll take care of remaining JUnit4 rules for
Cassandra/ElasticSearch/Mongo etc.


On Tue, Dec 3, 2019 at 1:23 PM Vladimir Sitnikov <
sitnikov.vladimir@gmail.com> wrote:

> Ok, I think PR1621 is ready to go:
> https://github.com/apache/calcite/pull/1621
> I'm inclined to merge it shortly
>
> The PR converts most of the tests, and by default, it allows JUnit5 only.
>
> Certain modules still have "non-trivial" JUnit4 code, and there was an
> agreement to convert them later.
> The modules receive junit4 dependency via junit4=true in gradle.properties.
>
> ---
>
> The most "controversial" change from my point of view is the removal of
> *Suite* classes.
> In other words, I have dropped classes like
> CalciteSuite, FileSuite, PlusSuite, and so on.
>
> I guess it is OK, as both Gradle and JUnit5 provide lots of ways to launch
> tests (e.g. tags are good).
> The good thing about the removal is it would reduce the number of
> merge-conflicts,
> and it would simplify adding new test classes.
>
> ---
>
> The most notable difference between junit4 and junit5 .assert* is that
> junit4 has (message, expected, actual)
> argument order while junit5 uses (expected, actual, message).
> The latter is better when message is big (e.g. concatenated), and junit5
> allows to use lambda,
> so the message is built only when assertion fails.
>
> I've performed argument shuffling with IntelliJ IDEA's "structural search
> and replace", so it should be OK.
>
> Vladimir
>

Re: JUnit 4 and JUnit 5 annotations do not mix well

Posted by Vladimir Sitnikov <si...@gmail.com>.
Ok, I think PR1621 is ready to go:
https://github.com/apache/calcite/pull/1621
I'm inclined to merge it shortly

The PR converts most of the tests, and by default, it allows JUnit5 only.

Certain modules still have "non-trivial" JUnit4 code, and there was an
agreement to convert them later.
The modules receive junit4 dependency via junit4=true in gradle.properties.

---

The most "controversial" change from my point of view is the removal of
*Suite* classes.
In other words, I have dropped classes like
CalciteSuite, FileSuite, PlusSuite, and so on.

I guess it is OK, as both Gradle and JUnit5 provide lots of ways to launch
tests (e.g. tags are good).
The good thing about the removal is it would reduce the number of
merge-conflicts,
and it would simplify adding new test classes.

---

The most notable difference between junit4 and junit5 .assert* is that
junit4 has (message, expected, actual)
argument order while junit5 uses (expected, actual, message).
The latter is better when message is big (e.g. concatenated), and junit5
allows to use lambda,
so the message is built only when assertion fails.

I've performed argument shuffling with IntelliJ IDEA's "structural search
and replace", so it should be OK.

Vladimir

Re: JUnit 4 and JUnit 5 annotations do not mix well

Posted by Stamatis Zampetakis <za...@gmail.com>.
Thanks Andrei and Vladimir for pushing this forward.

I'm +1 for complete migration to JUnit 5. Having both may lead into
problems as the one mentioned in the beginning of this thread.
More general using two versions of the same library at the same time
usually leads to problems.

I checked the code and it seems that we have ~11 uses of @RunWith and ~3
uses of @Rule so it should be feasible to migrate all of them quite fast.
I already migrated a couple of parameterized tests to JUnit 5 so I can help
if needed.





On Mon, Dec 2, 2019 at 11:53 PM Vladimir Sitnikov <
sitnikov.vladimir@gmail.com> wrote:

> Andrei>Perhaps split it into two parts:
>
> That makes sense.
> parameterized and classrules seem to be not that trivial to migrate.
>
> Then I would revert several changes in
> https://github.com/apache/calcite/pull/1621 and it would become a
> committable PR.
>
> Vladimir
>

Re: JUnit 4 and JUnit 5 annotations do not mix well

Posted by Vladimir Sitnikov <si...@gmail.com>.
Andrei>Perhaps split it into two parts:

That makes sense.
parameterized and classrules seem to be not that trivial to migrate.

Then I would revert several changes in
https://github.com/apache/calcite/pull/1621 and it would become a
committable PR.

Vladimir

Re: JUnit 4 and JUnit 5 annotations do not mix well

Posted by Andrei Sereda <an...@sereda.cc>.
I also lean towards updating existing tests to JUnit5. Perhaps split it
into two parts:

1. The ones that can be migrated easily (replacing annotations, minimal
manual intervention)
2. Existing JUnit 4 rules / parameterized tests

JUnit4 and JUnit5 can coexists and evaluated by the same engine.

On Mon, Dec 2, 2019 at 5:19 PM Vladimir Sitnikov <
sitnikov.vladimir@gmail.com> wrote:

> Stamatis> If we go for the bulk modifications approach why not migrating
> completely to JUnit5?
>
> Migration to JUnit5 seems to be ok.
>
> I've filed a draft that updates the APIs:
> https://github.com/apache/calcite/pull/1621
> It does not seem to touch too many places, and in most of the cases, it is
> a mere import adjustment.
>
> Vladimir
>

Re: JUnit 4 and JUnit 5 annotations do not mix well

Posted by Vladimir Sitnikov <si...@gmail.com>.
Stamatis> If we go for the bulk modifications approach why not migrating
completely to JUnit5?

Migration to JUnit5 seems to be ok.

I've filed a draft that updates the APIs:
https://github.com/apache/calcite/pull/1621
It does not seem to touch too many places, and in most of the cases, it is
a mere import adjustment.

Vladimir

Re: JUnit 4 and JUnit 5 annotations do not mix well

Posted by Andrei Sereda <an...@sereda.cc>.
> If we go for the bulk modifications approach why not migrating completely
to JUnit5?

Most of the tests can be migrated programmatically (string/replace) there
are some which need to be done manually (eg. JUnit special rules like
EmbeddedElasticsearchPolicy [1] and parameterized tests ).

> I remember that we had a discussion around the subject before but I
cannot find the thread at the moment.
See [2]  for relevant thread.

There were several concerns (at the time) :

1. Noise in commit history
2. Require use of newer versions of build tools (maven, IDE etc.)
3. Support of parallel execution of tests (from maven / gradle). Now
supported in 5.5

I'm happy to revive CALCITE-2457 if people are interested

https://github.com/apache/calcite/blob/ab136b5f76a4cb951e847fcba6b414c5e80dbbe6/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/EmbeddedElasticsearchPolicy.java
 [1]
https://lists.apache.org/thread.html/3c2e87359442eefbce67823926d3701e2541df1c798025be4a31e779@%3Cdev.calcite.apache.org%3E
 [2]



On Mon, Dec 2, 2019 at 2:16 PM Stamatis Zampetakis <za...@gmail.com>
wrote:

> If we go for the bulk modifications approach why not migrating completely
> to JUnit5?
>
> I remember that we had a discussion around the subject before but I cannot
> find the thread at the moment.
>
>
> On Mon, Dec 2, 2019, 5:58 PM Andrei Sereda <an...@sereda.cc> wrote:
>
> > Do you think it makes sense to perform a bulk string/replace for JUnit 4
> ->
> > 5 annotations ? At least for simple non-parameterized / no-rules tests.
> > This way IDE will automatically select correct annotations.
> >
> > I had some scripts prepared as part of CALCITE-2457 [1]
> >
> > https://issues.apache.org/jira/browse/CALCITE-2457 [1]
> >
> > On Mon, Dec 2, 2019 at 8:13 AM Michael Mior <mm...@apache.org> wrote:
> >
> > > Thanks for noting Stamatis!
> > >
> > > If a particular group of tests is migrating to JUnit 5, it also
> > > probably makes sense to use org.junit.jupiter.api.Assertions instead
> > > of org.junit.Assert. Although the latter is still supported, I think
> > > it makes sense to use things in the jupiter namespace where possible.
> > >
> > > --
> > > Michael Mior
> > > mmior@apache.org
> > >
> > >
> > > Le lun. 2 déc. 2019 à 07:53, Stamatis Zampetakis <za...@gmail.com> a
> > > écrit :
> > > >
> > > > Hello,
> > > >
> > > > At the moment, we can use both JUnit 4 and 5 annotations in our code
> > > base.
> > > > Lately, I spend some time on debugging a few test cases only to
> realize
> > > > that old and new APIs do not mix very well. If you encounter problems
> > > with
> > > > your annotations then consider checking your imports. The problem
> might
> > > lie
> > > > on the fact that you are using at the same time annotations from
> > > different
> > > > versions.
> > > >
> > > > Consider for example the following test.
> > > >
> > > > import org.junit.Assert;
> > > > import org.junit.Test;
> > > > import org.junit.jupiter.api.Disabled;
> > > >
> > > > public class SimpleTest {
> > > >
> > > >   @Test
> > > >   @Disabled
> > > >   public void test() {
> > > >     Assert.fail("Should not run because it is disabled");
> > > >   }
> > > > }
> > > >
> > > > Contrary to what people might expect the test above will run and will
> > > fail
> > > > with an assertion error. Changing the imports (see below) will make
> the
> > > > test behave as expected.
> > > >
> > > > package org.apache.calcite;
> > > >
> > > > import org.junit.Assert;
> > > > import org.junit.jupiter.api.Test;
> > > > import org.junit.jupiter.api.Disabled;
> > > >
> > > > public class SimpleTest {
> > > >
> > > >   @Test
> > > >   @Disabled
> > > >   public void test() {
> > > >     Assert.fail("Should not run because it is disabled");
> > > >   }
> > > > }
> > > >
> > > > Best,
> > > > Stamatis
> > >
> >
>

Re: JUnit 4 and JUnit 5 annotations do not mix well

Posted by Stamatis Zampetakis <za...@gmail.com>.
If we go for the bulk modifications approach why not migrating completely
to JUnit5?

I remember that we had a discussion around the subject before but I cannot
find the thread at the moment.


On Mon, Dec 2, 2019, 5:58 PM Andrei Sereda <an...@sereda.cc> wrote:

> Do you think it makes sense to perform a bulk string/replace for JUnit 4 ->
> 5 annotations ? At least for simple non-parameterized / no-rules tests.
> This way IDE will automatically select correct annotations.
>
> I had some scripts prepared as part of CALCITE-2457 [1]
>
> https://issues.apache.org/jira/browse/CALCITE-2457 [1]
>
> On Mon, Dec 2, 2019 at 8:13 AM Michael Mior <mm...@apache.org> wrote:
>
> > Thanks for noting Stamatis!
> >
> > If a particular group of tests is migrating to JUnit 5, it also
> > probably makes sense to use org.junit.jupiter.api.Assertions instead
> > of org.junit.Assert. Although the latter is still supported, I think
> > it makes sense to use things in the jupiter namespace where possible.
> >
> > --
> > Michael Mior
> > mmior@apache.org
> >
> >
> > Le lun. 2 déc. 2019 à 07:53, Stamatis Zampetakis <za...@gmail.com> a
> > écrit :
> > >
> > > Hello,
> > >
> > > At the moment, we can use both JUnit 4 and 5 annotations in our code
> > base.
> > > Lately, I spend some time on debugging a few test cases only to realize
> > > that old and new APIs do not mix very well. If you encounter problems
> > with
> > > your annotations then consider checking your imports. The problem might
> > lie
> > > on the fact that you are using at the same time annotations from
> > different
> > > versions.
> > >
> > > Consider for example the following test.
> > >
> > > import org.junit.Assert;
> > > import org.junit.Test;
> > > import org.junit.jupiter.api.Disabled;
> > >
> > > public class SimpleTest {
> > >
> > >   @Test
> > >   @Disabled
> > >   public void test() {
> > >     Assert.fail("Should not run because it is disabled");
> > >   }
> > > }
> > >
> > > Contrary to what people might expect the test above will run and will
> > fail
> > > with an assertion error. Changing the imports (see below) will make the
> > > test behave as expected.
> > >
> > > package org.apache.calcite;
> > >
> > > import org.junit.Assert;
> > > import org.junit.jupiter.api.Test;
> > > import org.junit.jupiter.api.Disabled;
> > >
> > > public class SimpleTest {
> > >
> > >   @Test
> > >   @Disabled
> > >   public void test() {
> > >     Assert.fail("Should not run because it is disabled");
> > >   }
> > > }
> > >
> > > Best,
> > > Stamatis
> >
>

Re: JUnit 4 and JUnit 5 annotations do not mix well

Posted by Andrei Sereda <an...@sereda.cc>.
Do you think it makes sense to perform a bulk string/replace for JUnit 4 ->
5 annotations ? At least for simple non-parameterized / no-rules tests.
This way IDE will automatically select correct annotations.

I had some scripts prepared as part of CALCITE-2457 [1]

https://issues.apache.org/jira/browse/CALCITE-2457 [1]

On Mon, Dec 2, 2019 at 8:13 AM Michael Mior <mm...@apache.org> wrote:

> Thanks for noting Stamatis!
>
> If a particular group of tests is migrating to JUnit 5, it also
> probably makes sense to use org.junit.jupiter.api.Assertions instead
> of org.junit.Assert. Although the latter is still supported, I think
> it makes sense to use things in the jupiter namespace where possible.
>
> --
> Michael Mior
> mmior@apache.org
>
>
> Le lun. 2 déc. 2019 à 07:53, Stamatis Zampetakis <za...@gmail.com> a
> écrit :
> >
> > Hello,
> >
> > At the moment, we can use both JUnit 4 and 5 annotations in our code
> base.
> > Lately, I spend some time on debugging a few test cases only to realize
> > that old and new APIs do not mix very well. If you encounter problems
> with
> > your annotations then consider checking your imports. The problem might
> lie
> > on the fact that you are using at the same time annotations from
> different
> > versions.
> >
> > Consider for example the following test.
> >
> > import org.junit.Assert;
> > import org.junit.Test;
> > import org.junit.jupiter.api.Disabled;
> >
> > public class SimpleTest {
> >
> >   @Test
> >   @Disabled
> >   public void test() {
> >     Assert.fail("Should not run because it is disabled");
> >   }
> > }
> >
> > Contrary to what people might expect the test above will run and will
> fail
> > with an assertion error. Changing the imports (see below) will make the
> > test behave as expected.
> >
> > package org.apache.calcite;
> >
> > import org.junit.Assert;
> > import org.junit.jupiter.api.Test;
> > import org.junit.jupiter.api.Disabled;
> >
> > public class SimpleTest {
> >
> >   @Test
> >   @Disabled
> >   public void test() {
> >     Assert.fail("Should not run because it is disabled");
> >   }
> > }
> >
> > Best,
> > Stamatis
>

Re: JUnit 4 and JUnit 5 annotations do not mix well

Posted by Michael Mior <mm...@apache.org>.
Thanks for noting Stamatis!

If a particular group of tests is migrating to JUnit 5, it also
probably makes sense to use org.junit.jupiter.api.Assertions instead
of org.junit.Assert. Although the latter is still supported, I think
it makes sense to use things in the jupiter namespace where possible.

--
Michael Mior
mmior@apache.org


Le lun. 2 déc. 2019 à 07:53, Stamatis Zampetakis <za...@gmail.com> a écrit :
>
> Hello,
>
> At the moment, we can use both JUnit 4 and 5 annotations in our code base.
> Lately, I spend some time on debugging a few test cases only to realize
> that old and new APIs do not mix very well. If you encounter problems with
> your annotations then consider checking your imports. The problem might lie
> on the fact that you are using at the same time annotations from different
> versions.
>
> Consider for example the following test.
>
> import org.junit.Assert;
> import org.junit.Test;
> import org.junit.jupiter.api.Disabled;
>
> public class SimpleTest {
>
>   @Test
>   @Disabled
>   public void test() {
>     Assert.fail("Should not run because it is disabled");
>   }
> }
>
> Contrary to what people might expect the test above will run and will fail
> with an assertion error. Changing the imports (see below) will make the
> test behave as expected.
>
> package org.apache.calcite;
>
> import org.junit.Assert;
> import org.junit.jupiter.api.Test;
> import org.junit.jupiter.api.Disabled;
>
> public class SimpleTest {
>
>   @Test
>   @Disabled
>   public void test() {
>     Assert.fail("Should not run because it is disabled");
>   }
> }
>
> Best,
> Stamatis