Your problem is that you created untestable code.
The point is: if a method creates the input it is working on itself ... then there is simply no testing that method. Yes, you could use PowerMock to gain control over the random class, but that is the completely wrong approach. PowerMock is simply something that nobody should be using (see here for some reasons behind that); if at all, you use it for 3rd party code that you can't change. But when we are talking about your own code, then it is much better to come up with testable designs; instead of creating a bad design, to then turn to PowerMock to somehow test it.
In general, you use dependency injection in order to gain control over the "data" that your methods will be working with; in this case, that could be as simple as:
public String formatStringIdFrom(int id) { ... }
If you really want to improve the quality of your code, I recommend you to step back for some 3, 5 hours and watch all the videos from this google tech series ... believe me, it is absolutely worth your time.
Some other notes:
- The essence of good unit tests are ... they always return the same result. This means: code that is based on random values ... should at least allow for providing a seed so that you can write test cases that are guaranteed to always see the same input. Unit tests that give you different results in each run ... are simply: not very helpful.
- id1() is a pretty useless name for a method. It doesn't tell you anything about what method is supposed to do
- Consider creating a concrete class that really represents IDs. Good design is about creating abstractions that allow you to deal with things in a more meaningful way. Just pushing around integers; and declaring that any 4-digit string made from such an integer is an "id" ... is simply a very naive approach.
- Finally: you can dramatically simplify the generation of 4 digit numbers; you only need:
int number = random.nextInt(9000) + 1000
directly gives you values between (1000, 9999).