---
title: "在Maven2中不可以运行Junit4中的Before吗？"
author: "iamlibo"
date: "2007-05-20T18:46:00Z"
tags: []
slug: "maven2-junit4-before"
draft: false
lastmod: "2007-05-20T18:46:00Z"
id: 274416
url: "https://iamlibo.iteye.com/blog/274416"
---

在Maven2中是可以运行Junit4的.但是@BeforeClass或@Before却不是每运行一个测试方法是运行.

测试代码如下:

        @BeforeClass  
        public static void setUp(){  
                System.out.println("beforeClass");  
        }  
          
        @Before  
        public void setUp1(){  
                System.out.println("before");  
        }  
        @Test  
        public void test1(){  
                System.out.println("test 1");  
        }  
          
        @Test  
        public void test2(){  
                System.out.println("test 2");  
        }

在Eclipse中运行结果是这样的:  
beforeClass  
before  
test 1  
before  
test 2  
这个结果也是正确的.但是在用Maven 测试的时候结果是这样的:

\-------------------------------------------------------  
T E S T S  
\-------------------------------------------------------  
Running cn.nowerp.platform.utils.StringUtilsTest  
test 1  
test 2  
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec

beforeclass 和before 都没有运行.这样一些初始化资源就不行了.

谁能给个正解？？