在JUnit Jupiter中使用自定义组合注释时,为@Autowired bean获取NPE

时间:2019-04-11 19:16:30

标签: java spring-test junit5

TransactionalIntegrationTest.java

    <configuration>
      <system.webServer>
        <webSocket enabled="false" />
        <handlers>
          <!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module -->
          <add name="iisnode" path="entry/server.js" verb="*" modules="iisnode"/>
        </handlers>
        <rewrite>
          <rules>
            <!-- Do not interfere with requests for node-inspector debugging -->
            <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
              <match url="^entry/server.js\/debug[\/]?" />
            </rule>
            <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
            <rule name="StaticContent">
              <action type="Rewrite" url="public{REQUEST_URI}"/>
            </rule>

            <!-- All other URLs are mapped to the node.js site entry point -->
            <rule name="DynamicContent">
              <conditions>
                 <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
              </conditions>
              <action type="Rewrite" url="entry/server.js"/>
            </rule>
          </rules>
        </rewrite>

        <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
        <security>
          <requestFiltering>
            <hiddenSegments>
              <remove segment="bin"/>
            </hiddenSegments>
          </requestFiltering>
        </security>

        <!-- Make sure error responses are left untouched -->
        <httpErrors existingResponse="PassThrough" />
      </system.webServer>
     </configuration>

MyTestTest .java

@TestMethodOrder(OrderAnnotation.class)
@SpringJUnitWebConfig(locations = { "classpath:service.xml","classpath:data.xml" })
@Tag("1")
public @interface TransactionalIntegrationTest {}

@TransactionalIntegrationTest public class MyTestTest { @Autowired protected CreateUser createUser; @BeforeEach public void setUp() throws Exception { createUser.createTimesheetUser(...)} --> NPE } 上获取NullPointerException。

如果我不使用元注释,那么它将正常工作。

MyTestTest.java

createUser

1 个答案:

答案 0 :(得分:2)

您可能会缺少@Retention声明,该声明允许诸如Spring和JUnit之类的框架在运行时查看注释。

按如下所示声明组成的注解应该起作用。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@TestMethodOrder(OrderAnnotation.class)
@SpringJUnitWebConfig(locations = { "classpath:service.xml", "classpath:data.xml" })
@Tag("1")
public @interface TransactionalIntegrationTest {
}