SCW Icons
hero bg no divider
Blog

修改 JUnit 5 的方法和类可见性

Alan Richardson
Published Dec 21, 2020
Last updated on Mar 09, 2026

Amending Method and Class Visibility for JUnit 5

One of the joys of programming is the constant learning required to keep up to date. One of the issues is that we build up familiarity and patterns of usage that can impact the adoption of new approaches. Sensei can help migration by identifying deprecated patterns and prompting us with the fix to use going forward.

As an example, when I migrated from JUnit 4 to JUnit 5, I was used to writing all my test classes and methods as public. But with JUnit 5 they can be package private.

e.g. instead of:

public class Junit5VisibilityTest {
    @Test
    public void thisDoesNotNeedToBePublic(){
        Assertions.assertTrue(true);
    }
}


I really want to write:

class Junit5VisibilityTest {
    @Test
    void thisDoesNotNeedToBePublic(){
        Assertions.assertTrue(true);
    }
}


It took me a while to build the muscle memory to code to this, and I still slip up once in a while.

Using Sensei

With Sensei I can create recipes that find the public methods and classes, and amend the declarations to be package private automatically.

To achieve this I created a recipe:

Name - JUnit: JUnit 5 test methods do not need to be public
Description - JUnit 5 test methods do not need public visibility
Level - Error


I classed it as Error because I want to stamp out this coding practice and I want higher visibility of the issue when I'm writing code in the IDE.

Amending the Class Declaration

To find the classes, I search for any class which has a child annotation of @Test from Junit 5 i.e. org.junit.jupiter.api.Test

And where the class has modifier public:

search:
  class:
    with:
      child:
        annotation:
          type: "org.junit.jupiter.api.Test"
    modifier: "public"

Then the quick fix changes the modifier to remove the visibility so that it is the default, and the default is package private which is what I'm looking for.

availableFixes:
- name: "remove public visibility from JUnit 5 Test class"
  actions:
  - changeModifiers:
      visibility: ""



Amending the Method Declarations

The method declaration amendment recipe is much the same as the class recipe.

First I search for public methods annotated with @Test from JUnit 5.

search:
  method:
    annotation:
      type: "org.junit.jupiter.api.Test"
    modifier: "public"


And then I change the modifier to be default visibility.

availableFixes:
- name: "Remove @Test method public visibility"
  actions:
  - changeModifiers:
      visibility: ""


Hint: Amending Multiple Methods

Sensei has the ability to apply the QuickFix to all the violations in the current file.

When I use alt+enter to apply the QuickFix.

If I expand the QuickFix name menu, I can see an option to:

"Fix All: 'JUnit: JUnit 5 test methods do not need to be public' problems in the file"

When I select that option then Sensei will amend all the occurrences of the problem, not just the one I select.


Remove Test Method Public Visibility

Amending the class

In the same way that a method does not need to be public, neither does the class.

I can create a recipe and a QuckFix to amend the class.

Name - JUnit: Junit 5 Test classes do not need to be public
Description - Junit 5 Test classes do not need to be public
Level - Error


When I find a class that is public and has a method with a @Test annotation. Then I want to change the visibility.

search:
  class:
    modifier: "public"
    anyOf:
    - child:
        method:
          annotation:
            type: "Test"


I can make the change to the class definition with the changeModifiers action again.

availableFixes:
- name: "Remove @Test class public visibility"
  actions:  
- changeModifiers:
      visibility: ""


Summary

A static analysis tool initially alerted me to this recommended approach in JUnit. But the static analysis tool didn't help me build the muscle memory to change my code as I program.

Use the 'Level' to alert you. When it is a problem I am trying to stamp out in my coding I initially make it 'Error' and then reduce this as I wean myself off the coding approach.

Remember you can use Sensei to fix all the issues in the current file at the same time, by using the drop-down menu option when applying the QuickFix.

By creating a Sensei recipe, I can see my old coding approach in real-time. And QuickFix it, to reinforce the approach if I occasionally slip up in my coding.

---

You can install Sensei from within IntelliJ using "Preferences \ Plugins" (Mac) or "Settings \ Plugins" (Windows)  then just search for "sensei secure code".

The source code and recipes for this can be found in the `sensei-blog-examples` repository in the Secure Code Warrior GitHub account, in the `junitexamples` module.



查看资源
查看资源

了解 Sensei 如何通过识别过时的模式并提示您提供后续使用的修复程序来帮助迁移。

对更多感兴趣?

Alan Richardson has more than twenty years of professional IT experience, working as a developer and at every level of the testing hierarchy from Tester through to Head of Testing. Head of Developer Relations at Secure Code Warrior, he works directly with teams, to improve the development of quality secure code. Alan is the author of four books including “Dear Evil Tester”, and “Java For Testers”. Alan has also created online training courses to help people learn Technical Web Testing and Selenium WebDriver with Java. Alan posts his writing and training videos on SeleniumSimplified.com, EvilTester.com, JavaForTesters.com, and CompendiumDev.co.uk.

learn more

Secure Code Warrior可以帮助您的组织在整个软件开发生命周期中保护代码,并营造一种将网络安全放在首位的文化。无论您是 AppSec 经理、开发人员、首席信息安全官还是任何与安全相关的人,我们都可以帮助您的组织降低与不安全代码相关的风险。

预订演示
分享到:
linkedin brandsSocialx logo
作者
Alan Richardson
Published Dec 21, 2020

Alan Richardson has more than twenty years of professional IT experience, working as a developer and at every level of the testing hierarchy from Tester through to Head of Testing. Head of Developer Relations at Secure Code Warrior, he works directly with teams, to improve the development of quality secure code. Alan is the author of four books including “Dear Evil Tester”, and “Java For Testers”. Alan has also created online training courses to help people learn Technical Web Testing and Selenium WebDriver with Java. Alan posts his writing and training videos on SeleniumSimplified.com, EvilTester.com, JavaForTesters.com, and CompendiumDev.co.uk.

分享到:
linkedin brandsSocialx logo

Amending Method and Class Visibility for JUnit 5

One of the joys of programming is the constant learning required to keep up to date. One of the issues is that we build up familiarity and patterns of usage that can impact the adoption of new approaches. Sensei can help migration by identifying deprecated patterns and prompting us with the fix to use going forward.

As an example, when I migrated from JUnit 4 to JUnit 5, I was used to writing all my test classes and methods as public. But with JUnit 5 they can be package private.

e.g. instead of:

public class Junit5VisibilityTest {
    @Test
    public void thisDoesNotNeedToBePublic(){
        Assertions.assertTrue(true);
    }
}


I really want to write:

class Junit5VisibilityTest {
    @Test
    void thisDoesNotNeedToBePublic(){
        Assertions.assertTrue(true);
    }
}


It took me a while to build the muscle memory to code to this, and I still slip up once in a while.

Using Sensei

With Sensei I can create recipes that find the public methods and classes, and amend the declarations to be package private automatically.

To achieve this I created a recipe:

Name - JUnit: JUnit 5 test methods do not need to be public
Description - JUnit 5 test methods do not need public visibility
Level - Error


I classed it as Error because I want to stamp out this coding practice and I want higher visibility of the issue when I'm writing code in the IDE.

Amending the Class Declaration

To find the classes, I search for any class which has a child annotation of @Test from Junit 5 i.e. org.junit.jupiter.api.Test

And where the class has modifier public:

search:
  class:
    with:
      child:
        annotation:
          type: "org.junit.jupiter.api.Test"
    modifier: "public"

Then the quick fix changes the modifier to remove the visibility so that it is the default, and the default is package private which is what I'm looking for.

availableFixes:
- name: "remove public visibility from JUnit 5 Test class"
  actions:
  - changeModifiers:
      visibility: ""



Amending the Method Declarations

The method declaration amendment recipe is much the same as the class recipe.

First I search for public methods annotated with @Test from JUnit 5.

search:
  method:
    annotation:
      type: "org.junit.jupiter.api.Test"
    modifier: "public"


And then I change the modifier to be default visibility.

availableFixes:
- name: "Remove @Test method public visibility"
  actions:
  - changeModifiers:
      visibility: ""


Hint: Amending Multiple Methods

Sensei has the ability to apply the QuickFix to all the violations in the current file.

When I use alt+enter to apply the QuickFix.

If I expand the QuickFix name menu, I can see an option to:

"Fix All: 'JUnit: JUnit 5 test methods do not need to be public' problems in the file"

When I select that option then Sensei will amend all the occurrences of the problem, not just the one I select.


Remove Test Method Public Visibility

Amending the class

In the same way that a method does not need to be public, neither does the class.

I can create a recipe and a QuckFix to amend the class.

Name - JUnit: Junit 5 Test classes do not need to be public
Description - Junit 5 Test classes do not need to be public
Level - Error


When I find a class that is public and has a method with a @Test annotation. Then I want to change the visibility.

search:
  class:
    modifier: "public"
    anyOf:
    - child:
        method:
          annotation:
            type: "Test"


I can make the change to the class definition with the changeModifiers action again.

availableFixes:
- name: "Remove @Test class public visibility"
  actions:  
- changeModifiers:
      visibility: ""


Summary

A static analysis tool initially alerted me to this recommended approach in JUnit. But the static analysis tool didn't help me build the muscle memory to change my code as I program.

Use the 'Level' to alert you. When it is a problem I am trying to stamp out in my coding I initially make it 'Error' and then reduce this as I wean myself off the coding approach.

Remember you can use Sensei to fix all the issues in the current file at the same time, by using the drop-down menu option when applying the QuickFix.

By creating a Sensei recipe, I can see my old coding approach in real-time. And QuickFix it, to reinforce the approach if I occasionally slip up in my coding.

---

You can install Sensei from within IntelliJ using "Preferences \ Plugins" (Mac) or "Settings \ Plugins" (Windows)  then just search for "sensei secure code".

The source code and recipes for this can be found in the `sensei-blog-examples` repository in the Secure Code Warrior GitHub account, in the `junitexamples` module.



查看资源
查看资源

填写下面的表格下载报告

我们希望获得您的许可,以便向您发送有关我们的产品和/或相关安全编码主题的信息。我们将始终非常谨慎地对待您的个人信息,绝不会出于营销目的将其出售给其他公司。

提交
scw success icon
scw error icon
要提交表单,请启用 “分析” Cookie。完成后,可以随意再次禁用它们。

Amending Method and Class Visibility for JUnit 5

One of the joys of programming is the constant learning required to keep up to date. One of the issues is that we build up familiarity and patterns of usage that can impact the adoption of new approaches. Sensei can help migration by identifying deprecated patterns and prompting us with the fix to use going forward.

As an example, when I migrated from JUnit 4 to JUnit 5, I was used to writing all my test classes and methods as public. But with JUnit 5 they can be package private.

e.g. instead of:

public class Junit5VisibilityTest {
    @Test
    public void thisDoesNotNeedToBePublic(){
        Assertions.assertTrue(true);
    }
}


I really want to write:

class Junit5VisibilityTest {
    @Test
    void thisDoesNotNeedToBePublic(){
        Assertions.assertTrue(true);
    }
}


It took me a while to build the muscle memory to code to this, and I still slip up once in a while.

Using Sensei

With Sensei I can create recipes that find the public methods and classes, and amend the declarations to be package private automatically.

To achieve this I created a recipe:

Name - JUnit: JUnit 5 test methods do not need to be public
Description - JUnit 5 test methods do not need public visibility
Level - Error


I classed it as Error because I want to stamp out this coding practice and I want higher visibility of the issue when I'm writing code in the IDE.

Amending the Class Declaration

To find the classes, I search for any class which has a child annotation of @Test from Junit 5 i.e. org.junit.jupiter.api.Test

And where the class has modifier public:

search:
  class:
    with:
      child:
        annotation:
          type: "org.junit.jupiter.api.Test"
    modifier: "public"

Then the quick fix changes the modifier to remove the visibility so that it is the default, and the default is package private which is what I'm looking for.

availableFixes:
- name: "remove public visibility from JUnit 5 Test class"
  actions:
  - changeModifiers:
      visibility: ""



Amending the Method Declarations

The method declaration amendment recipe is much the same as the class recipe.

First I search for public methods annotated with @Test from JUnit 5.

search:
  method:
    annotation:
      type: "org.junit.jupiter.api.Test"
    modifier: "public"


And then I change the modifier to be default visibility.

availableFixes:
- name: "Remove @Test method public visibility"
  actions:
  - changeModifiers:
      visibility: ""


Hint: Amending Multiple Methods

Sensei has the ability to apply the QuickFix to all the violations in the current file.

When I use alt+enter to apply the QuickFix.

If I expand the QuickFix name menu, I can see an option to:

"Fix All: 'JUnit: JUnit 5 test methods do not need to be public' problems in the file"

When I select that option then Sensei will amend all the occurrences of the problem, not just the one I select.


Remove Test Method Public Visibility

Amending the class

In the same way that a method does not need to be public, neither does the class.

I can create a recipe and a QuckFix to amend the class.

Name - JUnit: Junit 5 Test classes do not need to be public
Description - Junit 5 Test classes do not need to be public
Level - Error


When I find a class that is public and has a method with a @Test annotation. Then I want to change the visibility.

search:
  class:
    modifier: "public"
    anyOf:
    - child:
        method:
          annotation:
            type: "Test"


I can make the change to the class definition with the changeModifiers action again.

availableFixes:
- name: "Remove @Test class public visibility"
  actions:  
- changeModifiers:
      visibility: ""


Summary

A static analysis tool initially alerted me to this recommended approach in JUnit. But the static analysis tool didn't help me build the muscle memory to change my code as I program.

Use the 'Level' to alert you. When it is a problem I am trying to stamp out in my coding I initially make it 'Error' and then reduce this as I wean myself off the coding approach.

Remember you can use Sensei to fix all the issues in the current file at the same time, by using the drop-down menu option when applying the QuickFix.

By creating a Sensei recipe, I can see my old coding approach in real-time. And QuickFix it, to reinforce the approach if I occasionally slip up in my coding.

---

You can install Sensei from within IntelliJ using "Preferences \ Plugins" (Mac) or "Settings \ Plugins" (Windows)  then just search for "sensei secure code".

The source code and recipes for this can be found in the `sensei-blog-examples` repository in the Secure Code Warrior GitHub account, in the `junitexamples` module.



观看网络研讨会
开始吧
learn more

点击下面的链接并下载此资源的PDF。

Secure Code Warrior可以帮助您的组织在整个软件开发生命周期中保护代码,并营造一种将网络安全放在首位的文化。无论您是 AppSec 经理、开发人员、首席信息安全官还是任何与安全相关的人,我们都可以帮助您的组织降低与不安全代码相关的风险。

查看报告预订演示
查看资源
分享到:
linkedin brandsSocialx logo
对更多感兴趣?

分享到:
linkedin brandsSocialx logo
作者
Alan Richardson
Published Dec 21, 2020

Alan Richardson has more than twenty years of professional IT experience, working as a developer and at every level of the testing hierarchy from Tester through to Head of Testing. Head of Developer Relations at Secure Code Warrior, he works directly with teams, to improve the development of quality secure code. Alan is the author of four books including “Dear Evil Tester”, and “Java For Testers”. Alan has also created online training courses to help people learn Technical Web Testing and Selenium WebDriver with Java. Alan posts his writing and training videos on SeleniumSimplified.com, EvilTester.com, JavaForTesters.com, and CompendiumDev.co.uk.

分享到:
linkedin brandsSocialx logo

Amending Method and Class Visibility for JUnit 5

One of the joys of programming is the constant learning required to keep up to date. One of the issues is that we build up familiarity and patterns of usage that can impact the adoption of new approaches. Sensei can help migration by identifying deprecated patterns and prompting us with the fix to use going forward.

As an example, when I migrated from JUnit 4 to JUnit 5, I was used to writing all my test classes and methods as public. But with JUnit 5 they can be package private.

e.g. instead of:

public class Junit5VisibilityTest {
    @Test
    public void thisDoesNotNeedToBePublic(){
        Assertions.assertTrue(true);
    }
}


I really want to write:

class Junit5VisibilityTest {
    @Test
    void thisDoesNotNeedToBePublic(){
        Assertions.assertTrue(true);
    }
}


It took me a while to build the muscle memory to code to this, and I still slip up once in a while.

Using Sensei

With Sensei I can create recipes that find the public methods and classes, and amend the declarations to be package private automatically.

To achieve this I created a recipe:

Name - JUnit: JUnit 5 test methods do not need to be public
Description - JUnit 5 test methods do not need public visibility
Level - Error


I classed it as Error because I want to stamp out this coding practice and I want higher visibility of the issue when I'm writing code in the IDE.

Amending the Class Declaration

To find the classes, I search for any class which has a child annotation of @Test from Junit 5 i.e. org.junit.jupiter.api.Test

And where the class has modifier public:

search:
  class:
    with:
      child:
        annotation:
          type: "org.junit.jupiter.api.Test"
    modifier: "public"

Then the quick fix changes the modifier to remove the visibility so that it is the default, and the default is package private which is what I'm looking for.

availableFixes:
- name: "remove public visibility from JUnit 5 Test class"
  actions:
  - changeModifiers:
      visibility: ""



Amending the Method Declarations

The method declaration amendment recipe is much the same as the class recipe.

First I search for public methods annotated with @Test from JUnit 5.

search:
  method:
    annotation:
      type: "org.junit.jupiter.api.Test"
    modifier: "public"


And then I change the modifier to be default visibility.

availableFixes:
- name: "Remove @Test method public visibility"
  actions:
  - changeModifiers:
      visibility: ""


Hint: Amending Multiple Methods

Sensei has the ability to apply the QuickFix to all the violations in the current file.

When I use alt+enter to apply the QuickFix.

If I expand the QuickFix name menu, I can see an option to:

"Fix All: 'JUnit: JUnit 5 test methods do not need to be public' problems in the file"

When I select that option then Sensei will amend all the occurrences of the problem, not just the one I select.


Remove Test Method Public Visibility

Amending the class

In the same way that a method does not need to be public, neither does the class.

I can create a recipe and a QuckFix to amend the class.

Name - JUnit: Junit 5 Test classes do not need to be public
Description - Junit 5 Test classes do not need to be public
Level - Error


When I find a class that is public and has a method with a @Test annotation. Then I want to change the visibility.

search:
  class:
    modifier: "public"
    anyOf:
    - child:
        method:
          annotation:
            type: "Test"


I can make the change to the class definition with the changeModifiers action again.

availableFixes:
- name: "Remove @Test class public visibility"
  actions:  
- changeModifiers:
      visibility: ""


Summary

A static analysis tool initially alerted me to this recommended approach in JUnit. But the static analysis tool didn't help me build the muscle memory to change my code as I program.

Use the 'Level' to alert you. When it is a problem I am trying to stamp out in my coding I initially make it 'Error' and then reduce this as I wean myself off the coding approach.

Remember you can use Sensei to fix all the issues in the current file at the same time, by using the drop-down menu option when applying the QuickFix.

By creating a Sensei recipe, I can see my old coding approach in real-time. And QuickFix it, to reinforce the approach if I occasionally slip up in my coding.

---

You can install Sensei from within IntelliJ using "Preferences \ Plugins" (Mac) or "Settings \ Plugins" (Windows)  then just search for "sensei secure code".

The source code and recipes for this can be found in the `sensei-blog-examples` repository in the Secure Code Warrior GitHub account, in the `junitexamples` module.



目录

下载PDF
查看资源
对更多感兴趣?

Alan Richardson has more than twenty years of professional IT experience, working as a developer and at every level of the testing hierarchy from Tester through to Head of Testing. Head of Developer Relations at Secure Code Warrior, he works directly with teams, to improve the development of quality secure code. Alan is the author of four books including “Dear Evil Tester”, and “Java For Testers”. Alan has also created online training courses to help people learn Technical Web Testing and Selenium WebDriver with Java. Alan posts his writing and training videos on SeleniumSimplified.com, EvilTester.com, JavaForTesters.com, and CompendiumDev.co.uk.

learn more

Secure Code Warrior可以帮助您的组织在整个软件开发生命周期中保护代码,并营造一种将网络安全放在首位的文化。无论您是 AppSec 经理、开发人员、首席信息安全官还是任何与安全相关的人,我们都可以帮助您的组织降低与不安全代码相关的风险。

预订演示下载
分享到:
linkedin brandsSocialx logo
资源中心

帮助您入门的资源

更多帖子
资源中心

帮助您入门的资源

更多帖子